3

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...

Community
  • 1
  • 1
e-on
  • 1,547
  • 8
  • 32
  • 65
  • Can you reach URL via web browser? – doganak Apr 16 '14 at 12:49
  • the application is built with .NET but you request an ASP page? – Cerveser Apr 16 '14 at 12:55
  • Yes, URL works no problem in the browser – e-on Apr 16 '14 at 12:55
  • @Cervesar - yes I know it sounds odd, but all the asp page does is hit an external service with postcode and house number and returns a string of all possibilities. I've tried also with a .txt file that contains the info I'm expecting, but same result. – e-on Apr 16 '14 at 12:57
  • 2
    Have you tried accessing that in http instead of https? – Jamby Apr 16 '14 at 13:14
  • I realized that your are trying to make https reuqest. It does not look like certification problem, but could you add following line into your code? http://stackoverflow.com/a/526730/503446 – doganak Apr 16 '14 at 13:16
  • Ah, Jamby! I had set it to https, because recently the whole server had been locked down and all requests were set to force to https, so I never thought that would work. It did - thank you so much. If you add that as a proper answer, I'll mark it as correct – e-on Apr 16 '14 at 13:26

2 Answers2

21

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.

tbone
  • 15,107
  • 3
  • 33
  • 40
6

Taken from Jamby's answer in the comments under question:

Have you tried accessing that in http instead of https?

Simple. Thanks

e-on
  • 1,547
  • 8
  • 32
  • 65