zoukankan      html  css  js  c++  java
  • httpWebRequest请求错误,基础连接已经关闭: 连接被意外关闭

    C# 用httpWebRequest 执行post请求出现“请求错误,基础连接已经关闭: 连接被意外关闭”,解决方法:

    System.Net.HttpWebRequest request;

    request.ProtocolVersion = HttpVersion.Version10; 

    ---------------------------------------------------------------

    GET方法:

    
    
    public string DoWebRequest(string url)
            {
                HttpWebResponse webResponse = null;
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.Method = "POST";
                string responseStr = null;
                webRequest.Timeout = 50000;
                webRequest.ContentType = "text/html; charset=gb2312";
                try
                {
                    //尝试获得要请求的URL的返回消息
                    webResponse = (HttpWebResponse)webRequest.GetResponse();
                }
                catch (WebException e)
                {
                    //发生网络错误时,获取错误响应信息
                    responseStr = "发生网络错误!请稍后再试";
                }
                catch (Exception e)
                {
                    //发生异常时把错误信息当作错误信息返回
                    responseStr = "发生错误:" + e.Message;
     
                }
                finally
                {
                    if (webResponse != null)
                    {
                        //获得网络响应流
                        using (StreamReader responseReader = new StreamReader(webResponse.GetResponseStream(), Encoding.GetEncoding("GB2312")))
                        {
                            responseStr = responseReader.ReadToEnd();//获得返回流中的内容
                        }
                        webResponse.Close();//关闭web响应流
                    }
                }
                return responseStr;
            }
    
    
    
    
     

    注意:url中的中文,要先用HttpUtility.UrlEncode("内容",编码) 用服务器接收的编码,编码一下。

    POST方法:

     
     private string DoWebRequestByPost(string url, string param)
            {
                HttpWebResponse webResponse = null;
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
                //使用post方式提交
                webRequest.Method = "POST";
                string responseStr = null;
                webRequest.Timeout = 50000;
                //要post的字节数组
                byte[] postBytes = encoding.GetBytes(param);
                
                webRequest.ContentType = "application/x-www-form-urlencoded;";
                webRequest.ContentLength = postBytes.Length;
     
                using (Stream reqStream = webRequest.GetRequestStream())
                {
                    reqStream.Write(postBytes, 0, postBytes.Length);
                }
                try
                {
                    //尝试获得要请求的URL的返回消息
                    webResponse = (HttpWebResponse)webRequest.GetResponse();
                }
                catch (Exception)
                {
                    //出错后直接抛出
                    throw;
                }
                finally
                {
                    if (webResponse != null)
                    {
                        //获得网络响应流
                        using (StreamReader responseReader = new StreamReader(webResponse.GetResponseStream(), encoding))
                        {
                            responseStr = responseReader.ReadToEnd();//获得返回流中的内容
                        }
                        webResponse.Close();//关闭web响应流
                    }
                }
                return responseStr;
            }
    
    encoding为服务器接收的编码,例如:Encoding.GetEncoding("GBK")等
    
    param post请求的参数 param1=123&param2=中国&param3=abc 这样的格式,中文部分不用使用编码,方法内转成byte[]时
    会进行编码。
    
    
  • 相关阅读:
    C#校验算法列举
    SuperSocket1.6电子书离线版
    C#检测系统是否激活[转自StackOverFlow]
    WSMBT Modbus & WSMBS Modbus 控件及注册机
    AU3获取系统激活信息
    JavaScript跨浏览器事件处理
    OAuth2的学习小结
    R学习日记——分解时间序列(季节性数据)
    R学习日记——分解时间序列(非季节性数据)
    Java内存分配原理
  • 原文地址:https://www.cnblogs.com/25miao/p/6699575.html
Copyright © 2011-2022 走看看