1

I want to go to download a string from a website, I made this php file to show an example.

(This won't work around my whole website)

The link http://swageh.co/information.php won't be downloaded using a webClient from any PC.

I prefer using a webClient. No matter what I try, it won't downloadString. It works fine on a browser.

It returns an error 500 An unhandled exception of type 'System.Net.WebException' occurred in System.dll

Additional information: The underlying connection was closed: An unexpected error occurred on a send. is the error

4
  • It's not uncommon for sites to inspect your session and headers before returning a file to download, perhaps try it out using a tool like postman that will allow you to tinker with the headers easily. Commented Jul 19, 2018 at 15:41
  • Please specify the error message returned with the error 500 (if any) Commented Jul 19, 2018 at 16:31
  • 1
    Please show the code you are using. Commented Jul 19, 2018 at 16:52
  • @deztructicus An unhandled exception of type 'System.Net.WebException' occurred in System.dll Additional information: The underlying connection was closed: An unexpected error occurred on a send. Commented Jul 19, 2018 at 23:09

2 Answers 2

1

Did you change something on the server-side?
All of the following options are working just fine for me as of right now (all return just "false" with StatusCode of 200):

 var client = new WebClient();
 var stringResult = client.DownloadString("http://swageh.co/information.php");

Also HttpWebRequest:

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://swageh.co/information.php");
 request.GetResponse().GetResponseStream();

Newer HttpClient:

var client = new HttpClient();

var req = new HttpRequestMessage(HttpMethod.Get, "http://swageh.co/information.php");

var res = client.SendAsync(req);

var stringResult = res.Result.Content.ReadAsStringAsync().Result;
Sign up to request clarification or add additional context in comments.

2 Comments

yes there is no 301 redirect now when you open the link in Chrome.
worked like a charm, still don't know why it suddenly stopped returning 301 because I didn't change any code.
1

it's because your website is responding with 301 Moved Permanently see Get where a 301 URl redirects to This shows how to automatically follow the redirect: Using WebClient in C# is there a way to get the URL of a site after being redirected? look at Christophe Debove's answer rather than the accepted answer.

Interestingly this doesn't work - tried making headers the same as Chrome as below, perhaps use Telerik Fiddler to see what is happening.

var strUrl = "http://theurl_inhere";
            var headers = new WebHeaderCollection();
            headers.Add("Accept-Language", "en-US,en;q=0.9");
            headers.Add("Cache-Control", "no-cache");
            headers.Add("Pragma", "no-cache");
            headers.Add("Upgrade-Insecure-Requests", "1");

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
            request.Method = "GET";
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            request.Accept = "text/html,application/xhtml+xml,application/xml; q = 0.9,image / webp,image / apng,*/*;q=0.8";
            request.Headers.Add( headers );
            request.AllowAutoRedirect = true;
            request.KeepAlive = true;
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream dataStream = response.GetResponseStream();

            var strLastRedirect = response.ResponseUri.ToString();

            StreamReader reader = new StreamReader(dataStream);
            string strResponse = reader.ReadToEnd();

            response.Close();

1 Comment

Didn't work for me, sadly. But I did get the error code.An unhandled exception of type 'System.Net.WebException' occurred in System.dll Additional information: The underlying connection was closed: An unexpected error occurred on a send. Returned the exact error code as the regular webClient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.