zoukankan      html  css  js  c++  java
  • HttpWebRequest使用总结

    HttpWebRequest的KeepAlive默认是true,如果使用的时候仅仅只是关闭流,不关闭网卡上的通道的话,第二个请求在TCP没有关闭的情况下是走同一个通道,此时本机的TCP通道就会抛异常出来,这是本机抛的错误。所以除了关闭本机的IO资源外,还要关闭网络资源。需要把KeepAlive设置成false就可以了。TCP通信结束后会自动关闭该请求所使用的通道。
    request.About() 是发现异常就断掉
    http是上层协议,底层还是走tcp的,如果不关闭的话,第二个http会默认走没有关闭的tcp。如果有并发的时候,数据就乱了。所以应该及时关闭tcp,每次开一个新端口。 

    public string PostToHttpService(string url, string jsonData, string userName, string password)

            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/json";
                request.Credentials = new NetworkCredential(userName, password);
                request.Timeout = 180000;//两分钟
                request.ReadWriteTimeout = 180000;//两分钟
                request.KeepAlive = false;
                byte[] datas = Encoding.UTF8.GetBytes(jsonData);
                request.ContentLength = datas.Length;

                try
                {
                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(datas, 0, datas.Length);
                    requestStream.Close();
                }
                catch (System.Net.ProtocolViolationException ex)
                {
                    request.Abort();
                }
                catch (System.Net.WebException ex)
                {
                    request.Abort();
                }
                catch (System.ObjectDisposedException ex)
                {
                    request.Abort();
                }
                catch (System.InvalidOperationException ex)
                {
                    request.Abort();
                }
                catch (System.NotSupportedException ex)
                {
                    request.Abort();
                }


                HttpWebResponse response = null;
                string responseDatas = string.Empty;
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                    Stream streamResponse = response.GetResponseStream();
                    using (StreamReader sr = new StreamReader(streamResponse))
                    {
                        responseDatas = sr.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    request.Abort();
                }
                finally
                {
                    if (response != null)
                    {
                        try
                        {
                            response.Close();
                        }
                        catch {
                            request.Abort();
                        }
                    }
                }
                return responseDatas;
            }

  • 相关阅读:
    【Leetcode_easy】922. Sort Array By Parity II
    【Leetcode_easy】925. Long Pressed Name
    【Leetcode_easy】872. Leaf-Similar Trees
    【Leetcode_easy】874. Walking Robot Simulation
    【Leetcode_easy】1128. Number of Equivalent Domino Pairs
    【VxWorks工程】基于opencv创建读取摄像头数据的工程error
    【Leetcode_easy】868. Binary Gap
    【Leetcode_easy】867. Transpose Matrix
    【Leetcode_easy】860. Lemonade Change
    第11章 拾遗5:IPv6和IPv4共存技术(3)_NAT-PT技术【全书完】
  • 原文地址:https://www.cnblogs.com/xiaohua19920/p/12367668.html
Copyright © 2011-2022 走看看