android - Multipart request is getting error code 400 java -
i'm requesting web service in android using multipartutility , request sent is:
post / http/1.1 connection: keep-alive enctype: multipart/form-data content-type: multipart/form-data;charset=utf-8;boundary=--===1487292689114=== accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 accept-encoding: gzip, deflate, br accept-language: es-419,es;q=0.8,en-gb;q=0.6,en;q=0.4 cache-control: max-age=0 upgrade-insecure-requests: 1 user-agent: dalvik/1.6.0 (linux; u; android 4.4.4; 5042a build/ktu84p) host: 192.168.10.171:8080 content-length: 990 payload: ----===1487292689114=== content-disposition: form-data; name="new_id" 171 ----===1487292689114=== content-disposition: form-data; name="file"; filename="img_20170216_195118.jpg" content-type: image/jpeg content-transfer-encoding: binary ���� ( ----===1487292689114===--
this service:
@post @path("/reportnewimage") @consumes(mediatype.multipart_form_data+"; charset=utf-8") @produces(mediatype.text_plain) public response setimage( @formdataparam("new_id") string new_id, @formdataparam("file") inputstream uploadedinputstream, @formdataparam("file") formdatacontentdisposition filedetail, @context httpheaders headers) { ... }
but i'm getting same error 400 bad requesting when connection made java android code. when make connection html form result successful. please me, reason?
edit:
request made using html form:
post / http/1.1 host: example.com connection: keep-alive content-length: 6564 cache-control: max-age=0 origin: http://localhost upgrade-insecure-requests: 1 user-agent: mozilla/5.0 (x11; linux x86_64) applewebkit/537.36 (khtml, gecko) ubuntu chromium/55.0.2883.87 chrome/55.0.2883.87 safari/537.36 content-type: multipart/form-data; boundary=----webkitformboundarynt8qjsidddgznij8 accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 referer: http://localhost/form.html accept-encoding: gzip, deflate, br accept-language: es-419,es;q=0.8,en-gb;q=0.6,en;q=0.4 ------webkitformboundarynt8qjsidddgznij8 content-disposition: form-data; name="new_id" 109 ------webkitformboundarynt8qjsidddgznij8 content-disposition: form-data; name="file"; filename="apple.jpg" content-type: image/jpeg ����jfifhh��c ... �������1s�����k�"t�i)���ti6>�d ���� ------webkitformboundarynt8qjsidddgznij8--
and httpurlconnection android:
url url = new url(requesturl); httpconn = (httpurlconnection) url.openconnection(); httpconn.setrequestmethod("post"); httpconn.setusecaches(false); httpconn.setdooutput(true); // indicates post method httpconn.setdoinput(true); httpconn.setrequestproperty("content-type", "multipart/form-data; boundary=" + boundary); outputstream = httpconn.getoutputstream(); writer = new printwriter(new outputstreamwriter(outputstream, charset), true); writer.flush(); writer.close(); // checks server's status code first int status = httpconn.getresponsecode(); if (status == httpurlconnection.http_ok) { bufferedreader reader = new bufferedreader(new inputstreamreader( httpconn.getinputstream())); string line = null; while ((line = reader.readline()) != null) { response.append(line); } reader.close(); httpconn.disconnect(); } else { throw new ioexception("server returned non-ok status: " + status); }
edit 2:
i've figured out error appears if line compiled:
httpconn.setrequestproperty("content-type", "multipart/form-data; boundary=" + boundary);
else error code 415 because there no content-type
header defined.
the problem boundary. found out in documentation boundary can composed group of characters:
the mandatory parameter multipart content-type boundary parameter, consists of 1 70 characters set of characters known robust through email gateways, , not ending white space. (if boundary appears end white space, white space must presumed have been added gateway, , should deleted.) formally specified following bnf:
boundary := 0*69 bcharsnospace
bchars := bcharsnospace / " "
bcharsnospace := digit / alpha / "'" / "(" / ")" / "+" / "_" / "," / "-" / "." / "/" / ":" / "=" / "?" reference
nevertheless, since removed "=" character began work fine.
old bundary: --===1487292689114===
new boundary: --x1487292689114x
Comments
Post a Comment