xamarin.ios - Xamarin PCL HttpClient throws WebException: Error: ConnectFailure -
we're using mono httpclient send , receive data our app. works except when httpclient throws following exception:
[webexception: error: connectfailure (interrupted)] @ system.net.httpwebrequest.endgetresponse (iasyncresult asyncresult) <0x1004d0c80 + 0x00180> in <filename unknown> @ system.threading.tasks.taskfactory`1[tresult].fromasynccorelogic (iasyncresult iar, system.func`2 endfunction, system.action`1 endaction, system.threading.tasks.task`1 promise, boolean requiressynchronization) <0x1001dbe80 + 0x0005b> in <filename unknown> --- end of stack trace previous location exception thrown --- @ system.runtime.exceptionservices.exceptiondispatchinfo.throw () <0x10017d2b0 + 0x00028> in <filename unknown> @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess (system.threading.tasks.task task) <0x10017c7b0 + 0x000d3> in <filename unknown> @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification (system.threading.tasks.task task) <0x10017c700 + 0x0008b> in <filename unknown> @ system.runtime.compilerservices.taskawaiter.validateend (system.threading.tasks.task task) <0x10017c6a0 + 0x00047> in <filename unknown> @ system.runtime.compilerservices.configuredtaskawaitable`1+configuredtaskawaiter[tresult].getresult () <0x10017cdf0 + 0x0001b> in <filename unknown> @ system.net.http.httpclienthandler+<sendasync>c__async0.movenext () <0x100a4f070 + 0x00e53> in <filename unknown> --- end of stack trace previous location exception thrown --- @ system.runtime.exceptionservices.exceptiondispatchinfo.throw () <0x10017d2b0 + 0x00028> in <filename unknown> @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess (system.threading.tasks.task task) <0x10017c7b0 + 0x000d3> in <filename unknown> @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification (system.threading.tasks.task task) <0x10017c700 + 0x0008b> in <filename unknown> @ system.runtime.compilerservices.taskawaiter.validateend (system.threading.tasks.task task) <0x10017c6a0 + 0x00047> in <filename unknown> @ system.runtime.compilerservices.configuredtaskawaitable`1+configuredtaskawaiter[tresult].getresult () <0x10017cdf0 + 0x0001b> in <filename unknown> @ system.net.http.httpclient+<sendasyncworker>c__async0.movenext () <0x100a4de70 + 0x00373> in <filename unknown> message: [socketexception: interrupted] @ system.net.sockets.socket.connect (system.net.endpoint remoteep) <0x1004b0600 + 0x00170> in <filename unknown> @ system.net.webconnection.connect (system.net.httpwebrequest request) <0x1004e1e90 + 0x0044b> in <filename unknown>
clearly there no connection or connection being lost what's strange code sends or receives data api wrapped in try/catch exception must being thrown somewhere else - have idea where?
here's httpclient code:
private async task<result<t>> getasync<t>(string request) { using (var _client = new httpclient(new nativemessagehandler())) { _client.baseaddress = new uri(constants.base_uri); debug.writeline(_client.baseaddress + request); try { var result = await _client.getasync(request).withtimeout().configureawait(false); if (result.issuccessstatuscode) return await result.content.readasasync<result<t>>().configureawait(false); else return new result<t>(responsestatuscode.processfailed, default(t)); } catch (exception e) { return new result<t>(responsestatuscode.processfailed, default(t)); } } } private async task<result> getasync(string request) { using (var _client = new httpclient(new nativemessagehandler())) { _client.baseaddress = new uri(constants.base_uri); debug.writeline(_client.baseaddress + request); try { var result = await _client.getasync(request).withtimeout().configureawait(false); if (result.issuccessstatuscode) return await result.content.readasasync<result>().configureawait(false); else return new result(responsestatuscode.processfailed); } catch { return new result(responsestatuscode.processfailed); } } } private async task<result<tresponse>> postobjectasync<trequest, tresponse>(string request, trequest body) { using (var _client = new httpclient(new nativemessagehandler())) { _client.baseaddress = new uri(constants.base_uri); try { var result = await _client.postasjsonasync<trequest>(request, body).configureawait(false); if (result.issuccessstatuscode) return await result.content.readasasync<result<tresponse>>().configureawait(false); else return new result<tresponse>(responsestatuscode.processfailed, default(tresponse)); } catch { return new result<tresponse>(responsestatuscode.processfailed, default(tresponse)); } } } private async task<result> postobjectasync<t>(string request, t body) { using (var _client = new httpclient(new nativemessagehandler())) { _client.baseaddress = new uri(constants.base_uri); debug.writeline(_client.baseaddress + request); try { var result = await _client.postasjsonasync(request, body).configureawait(false); if (result.issuccessstatuscode) return await result.content.readasasync<result>().configureawait(false); else return new result(responsestatuscode.processfailed); } catch { return new result(responsestatuscode.processfailed); } } } private async task<result> postdataasync(string request, byte[] data, string contenttype) { using (var _client = new httpclient(new nativemessagehandler())) { var name = ""; var filename = ""; switch (contenttype) { case "image/jpeg": name = "image"; filename = "image.jpg"; break; case "audio/aac": name = "audio"; filename = "audio.aac"; break; } _client.baseaddress = new uri(constants.base_uri); debug.writeline(_client.baseaddress + request); var ticks = datetime.utcnow.ticks.tostring(); var content = new multipartformdatacontent("upload-----" + ticks); var datacontent = new bytearraycontent(data); datacontent.headers.contenttype = mediatypeheadervalue.parse(contenttype); content.add(datacontent, name, filename); try { var result = await _client.postasync(request, content).withtimeout(80000).configureawait(false); if (result.issuccessstatuscode) return await result.content.readasasync<result>().configureawait(false); else return new result(responsestatuscode.processfailed); } catch { return new result(responsestatuscode.processfailed); } } }
we're using modernhttpclient hence nativemessagehandler