C# HttpWebRequest GetRequestStream没反应超时解决办法

  前几天写的一个winform程序启动多线程调用HttpWebRequest POST请求远程网址验证数据,在window2003系统下运行没有问题,但是放到window2008系统或者window7下面运行的时候,出现了操作超时的问题,或者验证数据返回很慢,网速绝对没有问题的,同一台电脑,系统为window2003,然后通过vmware虚拟机安装了window2008和window7系统运行编写的软件。给朋友测试即使不是虚拟机安装的系统,也会出现超时请求或者返回数据很慢的问题。window2003系统则不存在这个问题。

C# HttpWebRequest GetRequestStream没反应超时解决办法

  后面在vmware安装的window7下安装visual C# express调试跟踪发现运行到ioStream = request.GetRequestStream()获取响应流以便写入数据的时候发现半天没反应,然后等了1分多钟后就出现了超时错误。

 

  按照网上找的方法,关闭请求对象,响应对象,设置 ServicePointManager.DefaultConnectionLimit ,但是在window2008和window7下都会出现超时问题,window2003下就是好好的。

    response.Close();
    request.Abort();
    response = null;
    request = null;

  ServicePointManager.DefaultConnectionLimit 默认为2,如果没有使用上面的代码释放请求连接,那么当前的Http的connection用完了,后续的GetResponse或GetRequestStream会超时死掉。

    ServicePointManager.DefaultConnectionLimit =1000;//默认为2

但是设置ServicePointManager.DefaultConnectionLimit =1000;还是没解决这个问题,还是运行到ioStream = request.GetRequestStream()这句就假死没有反应直到抛出超时错误。

 

  后面注意到请求的url是https加密协议的,不是普通的http请求,难道是这个问题。于是修改为请求普通的http页面测试,我靠( ‵o′)凸。。不再报超时错误了。看来请求https加密协议页面HttpWebRequest需要做一些额外的配置。

 

  按照网上找到的,在请求前按照如下设置,就不会出现超时错误了。但为毛window2003不设置也没问题,window2008或者window7会有这个问题,搞不懂。。

using System;
using System.Collections.Generic;
using System.Text;
using LiuLiangKa.Model;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.IO;
namespace LiuLiangKa.Tool
{
    public class HttpSend
    {
        /// <summary>
        /// ssl验证
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="certificate"></param>
        /// <param name="chain"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public static bool SecurityValidate(object sender,
                                X509Certificate certificate,
                                X509Chain chain,
                               SslPolicyErrors errors)
        {
            //直接确认,不然打不开,会出现超时错误
            return true;
        }
        /// <summary>
        /// 发送http请求查询
        /// </summary>
        /// <param name="card">流量卡实例</param>
        /// <returns></returns>
        public static AjaxResult Send(Card card)
        {
            AjaxResult r;
            try
            {
                Encoding utf8 = Encoding.UTF8;
                string postStr;
                byte[] postBin;
                HttpWebRequest request;
                HttpWebResponse response;
                Stream ioStream;
                #region Post
///////////////////////https设置
                ServicePointManager.DefaultConnectionLimit = 1000;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;//2013-08-23在window7下测试,只需要增加这个配置请求https就不会超时,下面这句省略也行。
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(SecurityValidate);//这个不需要也行,只需要设置ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
///////////////////////
                postStr = card.ToString();
                postBin = utf8.GetBytes(postStr);
                request = WebRequest.Create("https://xxxxxxx") as HttpWebRequest;///////////////////////https不设置SecurityProtocol和ServerCertificateValidationCallback会在request.GetRequestStream()这步超时
                //request = WebRequest.Create("http://xxxxx/json.asp") as HttpWebRequest;//非http不会出现超时错误
                request.Referer = "https://xxxxxxxx";
                request.Method = "post";
                request.KeepAlive = false;
                request.AllowAutoRedirect = false;//不允许重定向
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postBin.Length;
                ioStream = request.GetRequestStream();
                ioStream.Write(postBin, 0, postBin.Length);
                ioStream.Flush();
                ioStream.Close();
                response = request.GetResponse() as HttpWebResponse;
                ioStream = response.GetResponseStream();
                StreamReader sr = new StreamReader(ioStream, utf8);
                r = JSONToInstance.GetClassInstance(sr.ReadToEnd());
                sr.Close();
                ioStream.Close();
                response.Close();
                request.Abort();
                response = null;
                request = null;
                #endregion
            }
            catch (Exception ex)
            {
                r = new AjaxResultError();
                r.ty = "-1";
                r.voucher_pin = card.pin;
                ((AjaxResultError)r).errorMsg = ex.Message;
            }
            return r;
        }
    }
}

 

加支付宝好友偷能量挖...


原创文章,转载请注明出处:C# HttpWebRequest GetRequestStream没反应超时解决办法

评论(0)Web开发网
阅读(1687)喜欢(1)Asp.Net/C#/WCF