c# - HttpWebRequest.BeginGetRequestStream() best practice -
i'm working on async http crawler gathers data various services, , @ moment, i'm working threadpools serial httpwebrequest calls post/get data services.
i want transition on async web calls (begingetrequeststream , begingetresponse), need way response data , post stats (% completed write, when complete (when complete more important), etc). have event called object spawns/contains thread, signaling http data has been received. there event in webrequests can attach to call implemented event? seamless transition.
thanks help!!
you passing delegate (as part of async "state" parameter) needs called. after endgetresponsestream need , call delegate parameters need.
personally, since you're moving aysnc programming model (i assume better performance) suggest move workflow on to asynchronous well. model allows process results come in , fast possible without blocking whatsoever.
edit
on blog there article
httpwebrequest - asynchronous programming model/task.factory.fromasyc
on subject. i'm in process of writing it, i've presented class think use in situation. take @ either getasync method or postasync method depending on need.
public static void getasynctask(string url, action<httpwebrequestcallbackstate> responsecallback, string contenttype = "application/x-www-form-urlencoded")
notice responsecallback parameter? that's delegate talked earlier.
here example of how you'd call (i'm showing postasyn() method
var iterations = 100; (int = 0; < iterations; i++) { var postparameters = new namevaluecollection(); postparameters.add("data", i.tostring()); httpsocket.postasync(url, postparameters, callbackstate => { if (callbackstate.exception != null) throw callbackstate.exception; console.writeline(httpsocket.getresponsetext(callbackstate.responsestream)); }); }
the loop collection of urls. in case of don't need send (post) parameters , callback lambda see i'm writing console. here need, of send in delegate response processing done "elsewhere".
also callback method
action<httpwebrequestcallbackstate>
where httpwebrequestcallbackstate
custom class can modify include information need purposes. or modify signature to action.
Comments
Post a Comment