c# - Wait the end of HttpWebRequest.BeginGetRequestStream -
i'm trying use httpwebrequest.begingetrequeststream
method. in method i'm getting json
. need wait end of method (httpwebrequest.begingetrequeststream)
analyse json.
here code :
private string api_query(string url) { httpwebrequest requete = (httpwebrequest)httpwebrequest.create(url); requete.method = "post"; requete.contenttype = "application/x-www-form-urlencoded"; requete.begingetrequeststream(debutreponse, requete);//wait end of method //analyse json here return null; }
the problem don't know how wait end method. tried different things task , thread i'm not sure how correctly.
thanks help.
the problem don't know how wait end method.
there many approaches doing that. looks me want invoke request synchronously, i'd suggest calling getresponsestream
:
private string apiquery(string url) { httpwebrequest requete = (httpwebrequest)httpwebrequest.create(url); requete.method = "post"; requete.contenttype = "application/x-www-form-urlencoded"; using (var requeststream = requete.getrequeststream()) { // write request stream } using (var responsestream = requete.getresponse()) { // read respone stream, parsing out json. } }
edit:
as mentioned in comments, silverlight project. means don't have synchronous version of httpwebrequest
. instead, can use webclient
:
var webclient = new webclient() webclient.openreadcompleted += onuploadcompleted; webclient.openreadasync(url, data); private void onuploadcompleted(object sender, openreadcompletedeventargs e) { if (e.error != null) { // error, useful return; } using (var responsestream = e.result) { byte[] data = (byte[]) e.userstate; // read response stream. } }
Comments
Post a Comment