Search code examples
c#asp.netwebclient

The underlying connection was closed: - WebClient error


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


Solution

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

    Have you tried accessing that in http instead of https?

    Simple. Thanks