c# - .Net WebRequest Timeout versus TCP Timeout -


question

how configure tcp timeout single webrequest?

context

according docs, webrequest.timeout:

the length of time, in milliseconds, until request times out, or value timeout.infinite indicate request not time out. default value defined descendant class.

requesting web resource non-existent endpoint (iis without service bindings on requested port) fails different , constant timeout of 21 seconds.

according serverfault answer appears connect tcp timeout.

sample code

 public static async task<string> webrequesttostring(string requesturi, int timeoutmilliseconds)  {      var request = webrequest.create(requesturi) httpwebrequest;      request.keepalive = false;      request.timeout = timeoutmilliseconds;      request.readwritetimeout = timeoutmilliseconds;      using (var response = await request.getresponseasync() httpwebresponse)      {          // response stream          using (var reader = new streamreader(response.getresponsestream()))          {              var responsebody = await reader.readtoendasync();              return responsebody;          }      }  }   static void main(string[] args)  {      string uri = "http://10.15.1.24:8081/thiservicedoesnotexist/";      int timeoutmilliseconds = 10;       stopwatch sw = new stopwatch();      sw.start();      try      {          webrequesttostring(uri, timeoutmilliseconds).wait();      }      catch (aggregateexception ex)      {          console.writeline(ex.tostring());      }      sw.stop();       console.writeline("elaped {0}ms", sw.elapsedmilliseconds);      console.readkey();  } 

example output

  system.aggregateexception: 1 or more errors occurred. ---> system.net.webexcep   tion: unable connect remote server ---> system.net.sockets.socketexcep   tion: connection attempt failed because connected party did not r   espond after period of time, or established connection failed because connecte   d host has failed respond 10.15.1.24:8081      @ system.net.sockets.socket.endconnect(iasyncresult asyncresult)      @ system.net.servicepoint.connectsocketinternal(boolean connectfailure, sock   et s4, socket s6, socket& socket, ipaddress& address, connectsocketstate state,   iasyncresult asyncresult, exception& exception)      --- end of inner exception stack trace ---      @ system.net.httpwebrequest.endgetresponse(iasyncresult asyncresult)      @ system.threading.tasks.taskfactory`1.fromasynccorelogic(iasyncresult iar,   func`2 endfunction, action`1 endaction, task`1 promise, boolean requiressynchron   ization)   --- end of stack trace previous location exception thrown ---      @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)      @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernot   ification(task task)      @ system.runtime.compilerservices.taskawaiter`1.getresult()      @ timeouttests.program.<webrequesttostring>d__0.movenext() in \\vmware-host\   shared folders\documents\visual studio 2012\projects\timeouttests\timeouttests\p   rogram.cs:line 20      --- end of inner exception stack trace ---      @ system.threading.tasks.task.throwifexceptional(boolean includetaskcanceled   exceptions)      @ system.threading.tasks.task.wait(int32 millisecondstimeout, cancellationto   ken cancellationtoken)      @ system.threading.tasks.task.wait()      @ timeouttests.program.main(string[] args) in \\vmware-host\shared folders\d   ocuments\visual studio 2012\projects\timeouttests\timeouttests\program.cs:line 4   0   ---> (inner exception #0) system.net.webexception: unable connect remo   te server ---> system.net.sockets.socketexception: connection attempt failed b   ecause connected party did not respond after period of time, or e   stablished connection failed because connected host has failed respond 10.15.   1.24:8081      @ system.net.sockets.socket.endconnect(iasyncresult asyncresult)      @ system.net.servicepoint.connectsocketinternal(boolean connectfailure, sock   et s4, socket s6, socket& socket, ipaddress& address, connectsocketstate state,   iasyncresult asyncresult, exception& exception)      --- end of inner exception stack trace ---      @ system.net.httpwebrequest.endgetresponse(iasyncresult asyncresult)      @ system.threading.tasks.taskfactory`1.fromasynccorelogic(iasyncresult iar,   func`2 endfunction, action`1 endaction, task`1 promise, boolean requiressynchron   ization)   --- end of stack trace previous location exception thrown ---      @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)      @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernot   ification(task task)      @ system.runtime.compilerservices.taskawaiter`1.getresult()      @ timeouttests.program.<webrequesttostring>d__0.movenext() in \\vmware-host\   shared folders\documents\visual studio 2012\projects\timeouttests\timeouttests\p   rogram.cs:line 20<---    elaped 21169ms 

i doubtful timeout same tcp connection timeout. documentation of webrequest timeout follows :

the length of time, in milliseconds, until request times out, or value timeout.infinite indicate request not time out. default value defined descendant class.

that timeout on level of request, ie http. might connection server establised creating response takes long time. time can time out.

how webrequest handles , maps on tcp implementation dependent. tcp timeout connection establishment cannot infinite.

i read following : the timeout property indicates length of time, in milliseconds, until request times out , throws webexception. timeout property affects synchronous requests made getresponse method. time out asynchronous requests, use abort method.

you asynchronous!


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -