c# - universal windows app , making a WebRequest Method = GET -
does know how webresponse ?? method getresponse() obsolet, btw it's windows universal app.
uri uri = new uri("myuri"); httpclient httpclient = new httpclient(); httpwebrequest webrequest = (httpwebrequest)httpwebrequest.create(uri); httpclient.defaultrequestheaders.add("name", "value"); httpclient.defaultrequestheaders.accept.tryparseadd("application/json"); webrequest.method = "get"; httpwebresponse response = webrequest.getresponseasync(); streamreader streamreader1 = new streamreader(response.getresponsestream());
solved:
solved this: private async void start_click(object sender, routedeventargs e) {
response = new httpresponsemessage(); outputview.text = ""; httpclient.defaultrequestheaders.add("name", "value"); // value of 'inputaddress' set user , therefore untrusted input. // if can't create valid uri, // notify user incorrect input. uri resourceuri = new uri("myuri") string responsebodyastext; try { response = await httpclient.getasync(resourceuri); response.ensuresuccessstatuscode(); responsebodyastext = await response.content.readasstringasync(); } catch (exception ex) { // need convert int hresult hex string statustext.text = "error = " + ex.hresult.tostring("x") + " message: " + ex.message; responsebodyastext = ""; } // format http response display better responsebodyastext = responsebodyastext.replace("<br>", environment.newline); outputview.text = responsebodyastext;
you need include await
keyword.
httpwebresponse response = await webrequest.getresponseasync();
Comments
Post a Comment