用HttpWebRequest模拟发送https请求,提示错误: 基础连接已关闭,接受时发生错误
在本地测试ok,开发环境为64位win7 vs2013 发布到windows server 2008 x86系统上运行一直报错。。
下载2008 r2 x64运行发现数据正常。。
public static string GetUrl(string url)
{
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
System.Console.WriteLine("https connections.....");
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
// 这里设置了协议类型。
//ServicePointManager.SecurityProtocol =SecurityProtocolType.Tls12;// (SecurityProtocolType)3072;// SecurityProtocolType.Tls1.2;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //(SecurityProtocolType)768 | (SecurityProtocolType)3072
ServicePointManager.CheckCertificateRevocationList = true;
ServicePointManager.DefaultConnectionLimit = 100;
ServicePointManager.Expect100Continue = false;
}
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
//增加下面两个属性即可
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "GET"; //使用get方式发送数据
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = "";
request.AllowAutoRedirect = true;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
request.Accept = "*/*";
//获取网页响应结果
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//Thread.Sleep(2000);
Stream stream = response.GetResponseStream();
//client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
string result = string.Empty;
using (StreamReader sr = new StreamReader(stream))
{
result = sr.ReadToEnd();
}
if (request != null)
{
request.Abort();
}
if(response !=null)
{
response.Close();
}
return result;
}