HttpWebRequest Timeout

The client application with the REST style of popular, direct service by calling HttpWebRequest more and more. They came up with some experiences may require time-consuming investigation, hoping to help you. 


1. Spent HttpWebRequest to Abort () or put Response.Close () 
otherwise it will lead to a request Timeout. (HttpWebRequest.Method default is GET)

 

static   void  Main( string [] args)  

{  

for  ( int  i = 0; i < 10; i++)  

    {  

        Console.Write( "[{0}] Request - " , i + 1);  

        TryGet( "https://login.live.com/" );  

    }  

    Console.Read();  

}  

static   void  TryGet( object  obj)  

{  

try   

    {  

        HttpWebRequest webReq =  null ;  

string  url = ( string )obj;  

        webReq = (HttpWebRequest)HttpWebRequest.Create(url);  

        webReq.Timeout = 20 * 1000;  

        var resp = webReq.GetResponse()  as  HttpWebResponse;  

        resp.Close();  

        Console.WriteLine( "Get Response StatusCode: {0}({1})" ,   

            resp.StatusCode, ( int )resp.StatusCode);  

    }  

catch  (WebException we)  

    {  

        Console.WriteLine( "Get Response StatusCode: {0}({1})" ,  

            we.Status, ( int )we.Status);  

    }  

catch  (Exception ex)  

    {  

        Console.WriteLine(ex);  

    }  

}  

 


The above code will appear Timeout start from the 3rd Request, because the GetResponse Stream open not closed. 
 


Solution : The above code plus resp.Close (); or webReq.Abort (); it can be solved.  

2. When multiple threads call the HttpWebRequest, you need to set  ServicePointManager.DefaultConnectionLimit  number (default number of connections is 2). 
When the multi-threaded requests exceed the number of simultaneous connections Limit, GetResponse throws Timeout WebException.

 
// 用多线程同时发出4个请求   

WaitCallback methodTarget =  new  WaitCallback(TryGet);  

ThreadPool.QueueUserWorkItem(methodTarget,  "https://login.live.com/" );  

ThreadPool.QueueUserWorkItem(methodTarget,  "https://login.live.com/" );  

ThreadPool.QueueUserWorkItem(methodTarget,  "https://login.live.com/" );  

ThreadPool.QueueUserWorkItem(methodTarget,  "https://login.live.com/" );  

 

 

 

Solution : ServicePointManager.DefaultConnectionLimit = 100 disposed before the GetResponse (); 

3. When a request for SSL-based services, the default authentication behavior are defined in the ServicePointManager: 
ServicePointManager.CheckCertificateRevocationList = to true;

If the server certificate request does not support third-party certification, the request fails, if you want to completely trust the server certificate, you can 
CheckCertificateRevocationList set to false. 

4. HttpWebRequest properties can be configured in <system.net> configuration section, comprising WebProxy

 
<system.net>    
  <connectionManagement>    

  </connectionManagement>   

  <defaultProxy>    

    <proxy proxyaddress= "http://xxx.xxx.xxx.xxx:xxx"  bypassonlocal= "False" />    

  </defaultProxy>   

  <settings>    

      <httpWebRequest useUnsafeHeaderParsing= "true" />   

      <servicePointManager checkCertificateName= "true"      

                           checkCertificateRevocationList= "true"       

                           enableDnsRoundRobin= "true"      

                           expect100Continue= "true"        

                           useNagleAlgorithm= "true" />      

  </settings>   

</system.net> 

 

Original link: https://www.cnblogs.com/1971ruru/archive/2012/04/11/2442589.html?tdsourcetag=s_pctim_aiomsg#


1. Spent HttpWebRequest to Abort () or put Response.Close () 
otherwise it will lead to a request Timeout. (HttpWebRequest.Method default is GET)

 

static   void  Main( string [] args)  

{  

for  ( int  i = 0; i < 10; i++)  

    {  

        Console.Write( "[{0}] Request - " , i + 1);  

        TryGet( "https://login.live.com/" );  

    }  

    Console.Read();  

}  

static   void  TryGet( object  obj)  

{  

try   

    {  

        HttpWebRequest webReq =  null ;  

string  url = ( string )obj;  

        webReq = (HttpWebRequest)HttpWebRequest.Create(url);  

        webReq.Timeout = 20 * 1000;  

        var resp = webReq.GetResponse()  as  HttpWebResponse;  

        resp.Close();  

        Console.WriteLine( "Get Response StatusCode: {0}({1})" ,   

            resp.StatusCode, ( int )resp.StatusCode);  

    }  

catch  (WebException we)  

    {  

        Console.WriteLine( "Get Response StatusCode: {0}({1})" ,  

            we.Status, ( int )we.Status);  

    }  

catch  (Exception ex)  

    {  

        Console.WriteLine(ex);  

    }  

}  

 


The above code will appear Timeout start from the 3rd Request, because the GetResponse Stream open not closed. 
 


Solution : The above code plus resp.Close (); or webReq.Abort (); it can be solved.  

2. When multiple threads call the HttpWebRequest, you need to set  ServicePointManager.DefaultConnectionLimit  number (default number of connections is 2). 
When the multi-threaded requests exceed the number of simultaneous connections Limit, GetResponse throws Timeout WebException.

 
// 用多线程同时发出4个请求   

WaitCallback methodTarget =  new  WaitCallback(TryGet);  

ThreadPool.QueueUserWorkItem(methodTarget,  "https://login.live.com/" );  

ThreadPool.QueueUserWorkItem(methodTarget,  "https://login.live.com/" );  

ThreadPool.QueueUserWorkItem(methodTarget,  "https://login.live.com/" );  

ThreadPool.QueueUserWorkItem(methodTarget,  "https://login.live.com/" );  

 

 

 

解决方法:在GetResponse()之前设置 ServicePointManager.DefaultConnectionLimit = 100; 

3.  当请求一个基于SSL的服务时,默认的验证行为都在 ServicePointManager 定义: 
ServicePointManager.CheckCertificateRevocationList = true;

如果请求的服务端证书没有第三方的认证支持,则请求会失败,如果要完全信任服务端证书,则可以将 
CheckCertificateRevocationList  设为 false。 

4. 可以在 <system.net> 配置节中配置 HttpWebRequest 的属性,包括 WebProxy

 
<system.net>    
  <connectionManagement>    

  </connectionManagement>   

  <defaultProxy>    

    <proxy proxyaddress= "http://xxx.xxx.xxx.xxx:xxx"  bypassonlocal= "False" />    

  </defaultProxy>   

  <settings>    

      <httpWebRequest useUnsafeHeaderParsing= "true" />   

      <servicePointManager checkCertificateName= "true"      

                           checkCertificateRevocationList= "true"       

                           enableDnsRoundRobin= "true"      

                           expect100Continue= "true"        

                           useNagleAlgorithm= "true" />      

  </settings>   

</system.net> 

 

Original link: https://www.cnblogs.com/1971ruru/archive/2012/04/11/2442589.html?tdsourcetag=s_pctim_aiomsg#

Guess you like

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