Solution obstruction occurs when using HttpWebRequest

HttpWebRequest used as follows:

The first: Use Using resources released

     ///  <Summary> 
        /// Http request to return the Get Data
         ///  </ Summary> 
        ///  <param name = "URL"> Http request the URL </ param> 
        ///  <Returns> return results Http request </ Returns> 
        public  String HttpGetMethod ( String URL, int timeOut = 10 ) 
        { 
            String strResult = null ;
             the try 
            { 
                IF ( String .IsNullOrEmpty (URL)) 
                { 
                    return  null ;  
                }
                the HttpWebRequest Request= (HttpWebRequest)WebRequest.Create(url);
                request.Timeout = timeOut * 1000;
                using (HttpWebResponse wb = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream stream = wb.GetResponseStream())
                    {
                        using (StreamReader streamReader = new StreamReader(stream, Encoding.UTF8))
                        {
                            strResult = streamReader.ReadToEnd();
                        }
                    }
                } 
            } 
            The catch  (Exception ex)
            { 
                the throw ; // LoggerService.Log.Error ($ "{url} the Http request to return the Get data failed, because: ex.ToString {()}"); 
            }
             return strResult; 
        }

 

 

The second: use close to release resources

        ///  <Summary> 
        /// Http request to return the Get Data
         ///  </ Summary> 
        ///  <param name = "URL"> Http request the URL </ param> 
        ///  <Returns> return results Http request </ Returns> 
        public  String HttpGetMethod2 ( String URL, int timeOut = 10 ) 
        { 
            IF ( String .IsNullOrEmpty (URL)) 
            { 
                return  null ; 
            } 
            the HttpWebRequest Request = null ; 
            the HttpWebResponse Response =null;
            Stream streamReceive = null;
            StreamReader streamReader = null;
            string strResult = null;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Timeout = timeOut * 1000;
                ServicePointManager.DefaultConnectionLimit = 200;
                response = (HttpWebResponse)request.GetResponse();
                streamReceive = response.GetResponseStream();
                streamReader = new StreamReader(streamReceive, Encoding.UTF8);
                strResult = streamReader.ReadToEnd();
                streamReader.Close();
                streamReader.Dispose();
                streamReceive.Close();
                streamReceive.Dispose();
                response.Close();
                request.Abort();
            }
            catch (Exception ex)
            {
                throw;//   LoggerService.Log.Error($"Http Get请求{url}返回数据失败,原因:{ex.ToString()}");
            }
            finally
            {
                if (streamReader != null)
                {
                    streamReader.Close();
                    streamReader.Dispose();
                }
                if (streamReceive != null)
                {
                    streamReceive.Close();
                    streamReceive.Dispose();
                }
                if (response != null)
                {
                    response.Close();
                }
                
            }
            return strResult;
        }

 

Two basic writing HttpWebRequest use a variety of response, streamReceive, streamReader resources are released, what seemed to have no problem, in fact, just write a while loop, each time to ask for, you will find, can not run a few times blocked the simple reason that there is a HttpWebRequest object is not released, in fact, often released and Response Stream is not enough, or at the client's Request to maintain, can not release possession of resources in a timely manner, but rather Net of GC.Collect () system garbage collector to recover, and therefore can not ensure the timely release of resources, it is generally very easy to block, there will be the last request in the process, leading to this request does not go out. HttpWebRequest timely release is to make HttpWebRequest timely release of resources when not needed, so without blocking can be reused. 

In the program finally add the following sentence in a ok.

if (request != null)
  {
     request.Abort();
  }

 

Guess you like

Origin www.cnblogs.com/1175429393wljblog/p/11583508.html