Reputation: 1605
I keep getting an error when I try and hit a separate asp page using WebClient
for downloading full address details from a postcode.
The underlying connection was closed: An unexpected error occurred on a send.
This only happens when on the server. Not only that, when I paste the URL into a browser, it works perfectly. Could this be a firewall setting?
Here is the code I'm using (taken from another post on here)
using (CookieAwareWebClient WC = new CookieAwareWebClient())
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3;
string data = WC.DownloadString("https://www.myUrl.com/postcode.asp?postcode=" + postcode + "&houseNumber=" + Server.HtmlEncode(txtHouseNumber.Text));
}
originally I was just using this, with the same result:
WebClient client = new WebClient();
string address = "https://www.myUrl.com/postcode.asp?postcode=" + postcode + "&houseNumber=" + Server.HtmlEncode(txtHouseNumber.Text);
string data = client.DownloadString(address);
The application is built with .NET 4/C#, and hosting on Windows Server 2003 with iis6.
If this is a firewall or security setting, what would it be? If not, any thoughts on a cause or workaround? Many thanks
UPDATE - ########################
Ok, I tried with HttpWebRequest as well, and I get an error on this second line:
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(new Uri("https://www.myUrl.com/postcode.asp?postcode=AZ11ZA&houseNumber=1"));
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
The error message contains:
System.Threading.Thread.AbortInternal() at System.Threading.Thread.Abort(Object stateInfo) at System.Web.HttpResponse.End() at MyApp.MyPage.WebClientTest()
Don't know if that will help anyone...
Upvotes: 4
Views: 16562
Reputation: 5186
In my case it happens when I try to access a https site with a Let's Encrypt certificate.
I am using a very old .NET version: .Net Version:2.0.50727.8974
Also, the OS is already old: Windows Server 2012 R2
Many answers are about to upgrade .NET. In my case I can't do that, neither change to underlying OS.
Any answer, found by Google and here, didn't help. Some helped in the past but now something must have happened and it got apparently more restrictive.
Because I wrap each system call into my own function in my case the solution was possible, even it's not really the best one. Fortunately I don't use that much the WebClient
so I am simply doing a DOS call with curl
:
private string getWebClientContent( string urlWithContent )
{
string downloadedContent;
Process curlCommand = new Process();
curlCommand.StartInfo.FileName = @"C:\foo\curl.exe";
curlCommand.StartInfo.Arguments = @"-k --user-agent ""foo and bar"" " + urlWithContent;
curlCommand.StartInfo.UseShellExecute = false;
curlCommand.StartInfo.RedirectStandardOutput = true;
curlCommand.StartInfo.CreateNoWindow = true;
try
{
curlCommand.Start();
}
catch ( Exception e )
{
// The exception is mostly thrown when curl wasn't found in the path.
downloadedContent = "";
}
downloadedContent = curlCommand.StandardOutput.ReadToEnd();
curlCommand.WaitForExit();
return downloadedContent;
}
Upvotes: 0
Reputation: 15493
Just thought I'd add this, which worked for me. Some sites or firewalls block SSL or TLS connections by their version. Lately, this means any version less than 1.2. So, I set the following before downloading a file from the web:
using (WebClient webClient = new WebClient())
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
ServicePointManager.DefaultConnectionLimit = 9999;
log.Info("Downloading file");
webClient.DownloadFile("https://somesitedomain/somefile.csv", outfile);
}
The default TLS versions used may depend on the .NET framework used, and the enums seem to be missing on .NET 4.0 (hence the 3072). More recent versions should be able to do something like
SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12
For more, see here.
Upvotes: 22
Reputation: 1605
Taken from Jamby's answer in the comments under question:
Have you tried accessing that in http instead of https?
Simple. Thanks
Upvotes: 6