asp.net web api - WebApi returns 417 exception on android HttpUrlConnection(post) -
im trying send , recieve data web server post method in android .
this url call method :
inputstream input = null; outputstream output = null; dataoutputstream printout; httpurlconnection connection = null; try { url url = new url(surl[0]); connection = (httpurlconnection) url.openconnection(); connection.setreadtimeout(180000); connection.setconnecttimeout(180000); connection.setdoinput(true); connection.setdooutput(true); connection.setrequestproperty("content-type", "application/json"); connection.setrequestproperty("accept", "application/json"); connection.setrequestmethod("post"); connection.connect(); jsonobject jsonparam = new jsonobject(); jsonparam.put("imei",imei); packagemanager manager = this.context.getpackagemanager(); packageinfo info = null; try { info = manager.getpackageinfo( this.context.getpackagename(), 0); } catch (namenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } string version = info.versionname; jsonparam.put("version",version); byte[] data_ = null; try { data_ = jsonparam.tostring().getbytes("utf-8"); } catch (unsupportedencodingexception e1) { e1.printstacktrace(); } string base64 = base64.encodetostring(data_, base64.default); printout = new dataoutputstream(connection.getoutputstream ()); printout.writebytes(base64); printout.flush (); printout.close (); if (connection.getresponsecode() != httpurlconnection.http_ok) { return "server returned http " + connection.getresponsecode() + " " + connection.getresponsemessage(); } int filelength = connection.getcontentlength(); // download file input = connection.getinputstream(); string apk_path = environment.getexternalstoragedirectory() + "/download/"; file dir = new file (apk_path); dir.mkdirs(); file _file = new file(dir, "mynew.apk"); boolean deleted = _file.delete(); output = new fileoutputstream(_file); byte data[] = new byte[4096]; long total = 0; int count; while ((count = input.read(data)) != -1) { // allow canceling button if (iscancelled()) { input.close(); return null; } total += count; // publishing progress.... if (filelength > 0) // if total length known publishprogress((int) (total * 100 / filelength)); output.write(data, 0, count); } } catch (exception e) { return e.tostring(); } { try { if (output != null) output.close(); if (input != null) input.close(); } catch (ioexception ignored) { } if (connection != null) connection.disconnect(); } return null; }
and returns 417 exception server.
im using webapi + squid proxy. put line in webconfig :
<system.net> <settings> <servicepointmanager expect100continue="false" /> </settings> </system.net>
but didnt help.
in fixed problem :
httpclient httpclient = new defaulthttpclient(); httpparams params = httpclient.getparams(); params.setparameter(coreprotocolpnames.use_expect_continue,false);
but how can use httpparams inside httpurlconnection ?!
just set false
static property system.net
:
system.net.servicepointmanager.expect100continue = false;
inspire form this answer.
Comments
Post a Comment