【发布时间】:2016-04-17 11:39:14
【问题描述】:
我只是想从各种使用 C# 的零售站点下载一些站点信息,只是主页的纯 HTML。 我对某些使用 https 的网站有疑问,有些网站可以正常工作,而对于其他网站,我得到以下异常
The underlying connection was closed: An unexpected error occurred on a send.
内部异常有
The handshake failed due to an unexpected packet format.
我认为这与 TLS 有关,但对于我的生活,我无法弄清楚。我已经阅读过 Stack Overflow 和 HttpWebRequest 文档。
下面是我用来调用网站的示例代码,如果有人对此有任何见解,我将不胜感激,这让我发疯了。
public HttpWebResponse GetWebResponse(HtmlVerb verb, Uri uri)
{
var request = System.Net.WebRequest.CreateHttp(uri);
request.Method = verb.ToString();
request.ProtocolVersion = HttpVersion.Version11;
request.CookieContainer = new CookieContainer();
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0";
request.Timeout = (int)TimeSpan.FromSeconds(10).TotalMilliseconds;
request.AllowAutoRedirect = true;
request.ServerCertificateValidationCallback = delegate { return true; };
request.Headers.Add("Accept-Language", "en-US,en;q=0.5");
request.Headers.Add("Accept-Encoding", "gzip, deflate");
request.ContentType = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.KeepAlive = false;
return request.GetResponse() as HttpWebResponse;
}
亲切的问候
【问题讨论】:
-
您是否在 URI 中指定了端口,如下所示:stackoverflow.com/questions/16895484/… ?
-
尝试使用blogs.technet.microsoft.com/netmon/p/downloads 或wireshark.org 诊断实际发生的情况
标签: c# ssl httpwebrequest