質問

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

役に立ちましたか?

解決 2

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

Have you tried accessing that in http instead of https?

Simple. Thanks

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top