ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
private HttpWebResponse GetResponse(string url, string accept, ref CookieContainer container, bool allowRedirect, string referer)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ServicePoint.Expect100Continue = false;
request.CookieContainer = container;
request.Method = WebRequestMethods.Http.Get;
request.AllowAutoRedirect = allowRedirect;
request.KeepAlive = true;
request.UserAgent = "Mozilla / 5.0(Windows NT 10.0; WOW64; Trident / 7.0; rv: 11.0) like Gecko";
request.Accept = accept;
request.Referer = referer;
request.Headers["Accept-Encoding"] = "gzip, deflate";
try
{
return (HttpWebResponse)request.GetResponse();
}
catch (WebException e)
{
if (e.Response == null)
{
throw;
}
return (HttpWebResponse)e.Response;
}
}
有一不受控的外部服务器,证书 CN 不匹配,用 fiddler 进行中间人,证书 CN 与之匹配了,代码能如预期运行。
不使用 fiddler ,在请求 html 的时候能正常获取内容,说明忽略证书错误的代码起作用了,但请求 json 的时候 request.GetResponse()会卡住,debug 时断点在这里,按 F10/F11 就没有后续了。。
不知问题出在何处。。
|