c# - WebRequest.GetResponse() Times Out After A Couple of Requests -
i'm calling third party web api update of our data on side. i've been submitting 5 jobs in quick succession and, without fail, first 2 requests working properly. last 3 never update. application seems indicating request timing out, want make sure i'm not messing on side.
i'm calling function below action<string, dictionary<string,object>> delegate
, i'm using begininvoke
call api asynchronously. don't care response. misunderstanding webrequest.getresponse()
or problem endpoint?
private void updatejobinfo(string jobid, dictionary<string, object> updates) { var postdata = getjsonencodedvalues(updates); var request = webrequest.create(string.format(jobresultendpoint, _username, jobid)); request.contenttype = "application/json; charset=utf-8"; request.method = webrequestmethods.http.put; request.headers[httprequestheader.authorization] = getauthenticationcredentials(); request.getrequeststream().write(encoding.ascii.getbytes(postdata), 0, encoding.ascii.getbytes(postdata).length); request.getresponse(); }
you're not disposing of response (or indeed request stream, although that's different matter). means you're leaving connection server open until finalizer happens notice response can finalized. connections pooled (by default) 2 connections per url. later requests waiting earlier responses finalized before can obtain connection.
better code:
// want? non-ascii data? byte[] binarypostdata = encoding.ascii.getbytes(postdata); using (var requeststream = request.getrequeststream()) { requeststream.write(binarypostdata, 0, binarypostdata.length); } using (var response = request.getresponse()) { // don't care response, have fetch // , dispose it. }
Comments
Post a Comment