c# - AllowAutoRedirect in windowsPhone -
in httpclienthandler have propertie allowautoredirect, on build app windowsphone throw exception:
httpclienthandler.allowautoredirect not supported on platform. please check httpclienthandler.supportsredirectconfiguration before using httpclienthandler.allowautoredirect.
i want prevent autoredirect. tried use httpwebrequest:
var client = (httpwebrequest) webrequest.create(connectionurl); client.headers["allowautoredirect"] = "false"; client.method = "get"; client.headers["useragent"] = @"mozilla/5.0 (windows nt 6.2; wow64) applewebkit/537.31 (khtml, gecko) chrome/26.0.1410.43 safari/537.31"; client.contenttype = "application/json"; client.headers["contentlength"] = client.tostring().length.tostring(); client.begingetresponse(callback, client); private void callback(iasyncresult ar) { var requeststate =(httpwebrequest) ar.asyncstate; using (var poststream = requeststate.endgetrequeststream(ar)) {} }
this code throw exception on endgetrequeststream: "value not fall within expected range" forward help.
i think receiving argumentexception: value not fall within expected range
because initiating begingetresponse()
on client doing endgetrequeststream()
in callback instead should call endgetresponse()
. setting allowautoredirect
works fine need fix code. try this:
var client = (httpwebrequest)webrequest.create(connectionurl); client.allowautoredirect = false; client.method = "get"; client.begingetresponse(callback, client); private void callback(iasyncresult ar) { var state = (httpwebrequest)ar.asyncstate; using (var response = state.endgetresponse(ar)) { var streamresponse = response.getresponsestream(); var streamread = new streamreader(streamresponse); var responsestring = streamread.readtoend(); } }
Comments
Post a Comment