Why i'm getting time out exception when using httpwebrequest and response and how should i handle it, so far without success ?


in original method without using statement , without try , catch , without timout set 10000.

i changed later since got exception i'm getting exception again.

the program working fine hour or , got exception again.

my program downloading website same file every 5 minutes.

httpwebrequest request;         int currentindex = 0;         void filedownloadradar(string uri, string filename)         {             if (splash != null)             {                 if (!splash.isdisposed)                     splash.updateprogressbar(0);             }             try             {                 request = (system.net.httpwebrequest)system.net.webrequest.create(uri);                 request.timeout = 10000;                 request.cookiecontainer = new cookiecontainer();                 request.allowautoredirect = true;                 using (httpwebresponse response = (httpwebresponse)request.getresponse())                 {                     long contentlength = response.contentlength;                     if (response.contenttype == "")                     {                         logger.write("contenttype empty download not fine !!!!!");                     }                     if ((response.statuscode == httpstatuscode.ok ||                         response.statuscode == httpstatuscode.moved ||                         response.statuscode == httpstatuscode.redirect) &&                         response.contenttype.startswith("image", stringcomparison.ordinalignorecase))                     {                         logger.write("contenttype not empty meaning download fine");                         using (stream inputstream = response.getresponsestream())                         using (stream outputstream = file.openwrite(filename))                         {                             byte[] buffer = new byte[4096];                             int bytesread;                                                         {                                 bytesread = inputstream.read(buffer, 0, buffer.length);                                 currentindex += bytesread;                                 double percentage = (double)currentindex / contentlength;                                 if (splash != null)                                 {                                     if (!splash.isdisposed)                                         splash.updateprogressbar((int)(percentage * 100));                                 }                                 outputstream.write(buffer, 0, bytesread);                             } while (bytesread != 0);                             if (splash != null)                             {                                 if (!splash.isdisposed)                                 {                                     splash.updateprogressbar(100);                                 }                             }                         }                      }                     else                     {                         timer1.stop();                         timer3.start();                     }                     if (splash == null)                         finishwebrequest();                 }             }             catch (webexception ex)             {                 if (ex.status == webexceptionstatus.timeout)                 {                     logger.write(ex.status.tostring());                 }             }         }

the excption on line:

using (httpwebresponse response = (httpwebresponse)request.getresponse())

webexception: the operation has timed out

the exception message:

system.net.webexception occurred   hresult=-2146233079   message=the operation has timed out   source=system   stacktrace:        @ system.net.httpwebrequest.getresponse()        @ mws.form1.filedownloadradar(string uri, string filename) in d:\c-sharp\download file\downloading-file-project-version-012\downloading file\form1.cs:line 899   innerexception:

line 899 same line exception throw on.


hi chocolade1972,

there 2 possibilities.

first:same goes response streams , maybe other objects you're using. check if have close or dispose method. if have one, wrap them. 

using (httpwebresponse wr = (httpwebresponse)httprequest.getresponse()) using (streamreader srd = new streamreader(wr.getresponsestream()) {     status= srd.readtoend().tostring(); }

second,

someone put httpwebrequest.abort() fix it.

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

for more detailed information, please refer http://stackoverflow.com/questions/2252762/getrequeststream-throws-timeout-exception-randomly

good luck !

kristin


we trying better understand customer views on social support experience, participation in interview project appreciated if have time. helping make community forums great place.
click here participate survey.



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'