苏飞论坛

标题: 基础连接已经关闭: 发送时发生错误 [打印本页]

作者: huangdejun1107    时间: 2015-6-30 14:14
标题: 基础连接已经关闭: 发送时发生错误
做微信授权的时候需要post网址,然后传到服务器上,一切正常,但是过了几个小时就报错了。。System.Net.WebException: 基础连接已经关闭: 发送时发生错误。 ---> System.IO.IOException: 从传输流收到意外的 EOF 或 0 个字节。


代码如下:
System.GC.Collect();
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }

            HttpWebRequest request = null;
            Stream stream = null;//用于传参数的流

            try
            {
                //如果是发送HTTPS请求  
                if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    request = WebRequest.Create(url) as HttpWebRequest;
                    //创建证书文件
                    System.Security.Cryptography.X509Certificates.X509Certificate objx509 = new System.Security.Cryptography.X509Certificates.X509Certificate(@"\\Oauth\zjs.cer");
                    //添加到请求里
                    request.ClientCertificates.Add(objx509);
                    request.ProtocolVersion = HttpVersion.Version10;
                }
                else
                {
                    request = WebRequest.Create(url) as HttpWebRequest;
                }

                request.Method = "POST";//传输方式
                request.ContentType = "application/x-www-form-urlencoded";//协议               
                request.UserAgent = DefaultUserAgent;//请求的客户端浏览器信息,默认IE               
                request.Timeout = 10000;//超时时间,写死6秒
                request.KeepAlive = false;
                System.Net.ServicePointManager.DefaultConnectionLimit = 1000;
                //随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空
                if (cookies != null)
                {
                    request.CookieContainer = new CookieContainer();
                    request.CookieContainer.Add(cookies);
                }

                //如果需求POST传数据,转换成utf-8编码
                byte[] data = requestEncoding.GetBytes(parameters);
                request.ContentLength = data.Length;

                stream = request.GetRequestStream();
                stream.Write(data, 0, data.Length);

                stream.Close();
                request.Abort();

            }
            catch (Exception ee)
            {
                //写日志
                //LogHelper.
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }
            }

            return request.GetResponse() as HttpWebResponse;



《--------------------------------------------------------》出错之后程序重新传一下就又好了,过几个小时又出问题了。就这么如此反复,我是没办法了。。求大神给看看。。





欢迎光临 苏飞论坛 (http://www.sufeinet.com/) Powered by Discuz! X3.4