C# HttpWebRequest请求https时GetRequestStream超时

使用C# HttpWebRequest请求https网址时,调用GetRequestStream会出现超时错误,在window2003没有问题,在windows2008 R2和windows 7系统下会出问题

但是2008 R2和window7系统下请求http不会超时,就是只有https会出现超时。

4个回答

//注意类添加下面2个名字空间
//using System.Net

.Security;
//using System.Security.Cryptography.X509Certificates;

    public static bool SecurityValidate(object sender,
                            X509Certificate certificate,
                            X509Chain chain,
                           SslPolicyErrors errors)
    {
        //直接确认,不然打不开,会出现超时错误
        return true;
    }
    public string DownloadHtml(string url){
                            HttpWebRequest request;

///////////////////////https设置
ServicePointManager.DefaultConnectionLimit = 1000;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(SecurityValidate);
///////////////////////
//request的相关代码...............

}

添加ServerCertificateValidationCallback试试

 //注意类添加下面2个名字空间
//using System.Net 

.Security;
//using System.Security.Cryptography.X509Certificates;

        public static bool SecurityValidate(object sender,
                                X509Certificate certificate,
                                X509Chain chain,
                               SslPolicyErrors errors)
        {
            //直接确认,不然打不开,会出现超时错误
            return true;
        }
        public string DownloadHtml(string url){
                                HttpWebRequest request;
///////////////////////https设置
                ServicePointManager.DefaultConnectionLimit = 1000;
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(SecurityValidate);
///////////////////////
//request的相关代码...............

}

https://bbs.csdn.net/topics/392180866

     if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                request = WebRequest.Create(url) as HttpWebRequest;
                request.ProtocolVersion = HttpVersion.Version11;
            }
            else
            {
                request = WebRequest.Create(url) as HttpWebRequest;
            }
qq_34068295
qq_34068295 good
大约 2 年之前 回复
u011606457
_1_1_7_ While the code works in Windows XP, in Windows 7, you must add this at the beginning: ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; // Use SecurityProtocolType.Ssl3 if needed for compatibility reasons
大约 2 年之前 回复

建议换一中方式吧

1. GET
public static String SendGetRetuen(string url)
{
try
{
System.Net.HttpWebRequest request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
request.Method = "GET";
// request.UserAgent = DefaultUserAgent;
System.Net.HttpWebResponse result = request.GetResponse() as System.Net.HttpWebResponse;
System.IO.StreamReader sr = new System.IO.StreamReader(result.GetResponseStream(), Encoding.UTF8);
string strResult = sr.ReadToEnd();
sr.Close();
//Console.WriteLine(strResult);
return strResult;
}
catch (Exception)
{
return "";
}
}

            或者

             /// <summary>
    /// 
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    public static string HttpWebGet(string url)
    {
        //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
        Encoding encoding = Encoding.UTF8;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";
        request.Accept = "text/html, application/xhtml+xml, */*";
        request.ContentType = "application/json";

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
        {
            return reader.ReadToEnd();
        }
    }

2.POST PUT DELETE

    public List<object> sendPostList(System.Collections.Specialized.NameValueCollection PostVars, string methodName)
    {

        string jsonText = null;
        List<object> JsonData = null;
        try
        {
            System.Net.WebClient WebClientObj = new System.Net.WebClient();
            //if (SessionService.SessionMark != null)
            //    WebClientObj.Headers.Add("Cookie", SessionService.SessionMark);//在给服务器发送消息时加上这个Session值

            string strCarInfoUrl = System.Configuration.ConfigurationManager.AppSettings[methodName].ToString();
            byte[] byRemoteInfo = WebClientObj.UploadValues(strCarInfoUrl, "POST", PostVars);

            jsonText = System.Text.Encoding.UTF8.GetString(byRemoteInfo);//这是获取返回信息

            if (jsonText != "{}" || jsonText != "[]")
            {
                JavaScriptSerializer json = new JavaScriptSerializer();
                JsonData = json.Deserialize<List<object>>(jsonText);

            }
        }
        catch (System.Exception e)
        {
            Logger.debug("Err:获取接口信息失败!" + methodName + e.Message.ToString());
        }
        return JsonData;

    }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问