windows 8 - C# HttpClient weird error -
i have following code runs no issues on windows phone 8, running on windows 8 results in error.
error:
exception received while submitting payload: @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter`1.getresult() @ fooproject.httphelper.<submitrequesttomobileanalytics>d__a.movenext() in c:\users\foo_000\documents\github\fooproject\httphelper.cs:line 135 inner exception is: @ system.net.connectstream.closeinternal(boolean internalcall, boolean aborting) @ system.net.connectstream.system.net.icloseex.closeex(closeexstate closestate) @ system.net.connectstream.dispose(boolean disposing) @ system.io.stream.close() @ system.net.http.httpclienthandler.<>c__displayclass8.<getrequeststreamcallback>b__6(task task)
code:
string jsonpayload = "<<<some json payload>>>"; using (httpclient httpclient = new httpclient()) { try { //converting json payload gzipped byte array byte[] payload = compressasbyte(jsonpayload); if (payload != null) { httprequestmessage request = new httprequestmessage(httpmethod.post, config.instance().trackingserver); request.content = new bytearraycontent(payload); request.content.headers.add("content-encoding", "gzip"); request.content.headers.add("content-type", "application/json"); httpresponsemessage response = await httpclient.sendasync(request); return (int)response.statuscode; } else { return 0; } } catch (system.exception e) { logger.log("exception received while submitting payload: " + e.stacktrace); if (e.innerexception != null) { logger.log("inner exception is: " + e.innerexception.stacktrace); } return 0; } }
for windows phone 8 app, referencing microsoft httpclient nuget repository. windows 8, packages system.net , system.net.http visible me.
for reason, same code running on phone works , on windows 8 tablet (simulator), throws error.
am missing using httpclient on windows 8?
i added following code, worked magic.
request.content.headers.add("content-length", payload.length.tostring());
i don't know why succeeded without line on windows phone 8.
but worked.
Comments
Post a Comment