why following code timeout second (and subsequent) time run?
the code hangs at:
using (stream objstream = request.getresponse().getresponsestream())
and causes webexception saying request has timed out.
i have tried webrequest
, httpwebrequest
edit: seems code falling on in request.getresponse()
edit: post suggests may gc issue --> http://www.vbforums.com/showthread.php?t=610043 - per post issue mitigated if fiddler open in background.
the server there , available requests.
private string getqlmresponse(string url) { httpwebrequest request = webrequest.create(url) httpwebrequest; request.credentials = new networkcredential(settings.default.licenseuser, settings.default.licensepassword); request.keepalive = false; request.timeout = 5000; request.proxy = null; // read stream string responsestring = string.empty; try { using (var response = request.getresponse()) { using (stream objstream = response.getresponsestream()) { using (streamreader objreader = new streamreader(objstream)) { responsestring = objreader.readtoend(); objreader.close(); } objstream.flush(); objstream.close(); } response.close(); } } catch (webexception ex) { throw new licenseserverunavailableexception(); } { request.abort(); request = null; gc.collect(); } return responsestring; }
thrown webexception is:
{"the operation has timed out"} [system.net.webexception]: {"the operation has timed out"} data: {system.collections.listdictionaryinternal} helplink: null innerexception: null message: "the operation has timed out" source: "system" stacktrace: " @ system.net.httpwebrequest.getresponse()\r\n @ iqx.licensing.license.getqlmresponse(string url) in c:\users\jd\svn\jd\products\development\jad.licensing\jad.licensing\license.cs:line 373" targetsite: {system.net.webresponse getresponse()}
update: ok following code works. servicepoint setting timeout near 4 minutes. changing servicepoint.connectionleasetimeout
on request object means request destroyed after 5000ms. , these 2 pages:
- http://blogs.msdn.com/b/adarshk/archive/2005/01/02/345411.aspx
http://msdn.microsoft.com/en-us/library/6hszazfz(v=vs.80).aspx
private string getqlmresponse(string url) { httpwebrequest request = webrequest.create(url) httpwebrequest; request.credentials = new networkcredential(settings.default.licenseuser, settings.default.licensepassword); request.keepalive = false; request.timeout = 5000; request.proxy = null; request.servicepoint.connectionleasetimeout = 5000; request.servicepoint.maxidletime = 5000; // read stream string responsestring = string.empty; try { using (webresponse response = request.getresponse()) { using (stream objstream = response.getresponsestream()) { using (streamreader objreader = new streamreader(objstream)) { responsestring = objreader.readtoend(); objreader.close(); } objstream.flush(); objstream.close(); } response.close(); } } catch (webexception ex) { throw new licenseserverunavailableexception(); } { request.abort(); } return responsestring; }
on heels of previous answers, wanted add couple more things. default httpwebrequest
allows 2 connections same host (this http 1.1 "niceness"),
yes, can overriden, no won't tell how in question, have ask 1 :) think ought @ this post.
i think still not quite disposing of resources connected httpwebrequest, connection pooling comes play , that's problem. wouldn't try fight 2 connections per server rule, unless have to.
as 1 of posters above noted, fiddler doing bit of disservice in case.
i'd add nice finally {}
clause after catch , make sure above post notes, streams flushed, closed , references request object set null.
please let know if helps.
Comments
Post a Comment