java - Posting data with Android and HTTPSURLConnection -
i realize there numerous posts how post json data using android , httpurlconnection. regardless of tried, post body seems never arrive / magically disappear. hoping advise me:
// encrypt post data string ciphertext = crypt.encrypt(postdata, encryptionkey); int postdatalength = ciphertext.getbytes(standardcharsets.utf_8).length; byte[] bcipertext = ciphertext.getbytes(standardcharsets.utf_8); // establish connection httpsurlconnection conn = (httpsurlconnection) url.openconnection(); // set request method conn.setrequestmethod("post"); // set headers (string key : headers.keyset()) { conn.setrequestproperty(key, headers.get(key).trim()); } // set content type - application/json! conn.setrequestproperty("content-type", "application/json"); // set utf-8 charset conn.setrequestproperty("accept-charset", "utf-8"); conn.addrequestproperty("content-type", "application/" + "post"); // don't cache data conn.setusecaches(false); // expect output conn.setdooutput(true); if (!postdata.isempty()) { conn.setdoinput(true); // set content length conn.setfixedlengthstreamingmode(postdatalength); printwriter out = new printwriter(conn.getoutputstream()); out.print(ciphertext); out.close(); }
sample ciphertext:
imupu3djzrtwofmaeazr5ovunu2jg8kjl4i3letidld18jg3ijg5m9vmon-jndqt3fqlpotorb3ibmwl3-ia3lx5ryjiwpi7ccbnhkovyhzemktgc_n_6hqu4hycmielxvi93nwgmasnpd00tngfvsf0g3blpgrq-gm5doxq6wrwkglspzghgqbk0ghxs3tndgubtymjzho3lkvr6k5nky9jfesyat7b7wo1-dfbvqr9doy16mjrslvkpzy_x1p63e9cqfnac1sp8yc2rvvxtoc_aq_oyqr1fzxhwi3z9wa=,j83haiwvsxdmtq3uxpzafq==
edit: problem persists okhttp:
request request = new request.builder() .url(url) .post(requestbody.create(mediatype.parse("text/plain; charset=utf-8"), ciphertext)) .build(); okhttpclient client = new okhttpclient(); response response = client.newcall(request).execute(); system.out.println(ciphertext); string output = response.body().string(); system.out.println(output); // decrypt string decryptedresponse = null; try { decryptedresponse = crypt.decrypt(output.tostring(), encryptionkey).trim(); } catch(exception e) { // if response wasn't encrypted, receive exception decryptedresponse = output.tostring(); } httpresponse resp = new httpresponse(); resp.setstatus(response.code());
php receiving request is, in essence, this:
print_r($_post);
you should consider using third party library work http requests. eg okhttp dramatically reduces code overhead , amount or errors:
string postbody = "" + "releases\n" + "--------\n" + "\n" + " * _1.0_ may 6, 2013\n" + " * _1.1_ june 15, 2013\n" + " * _1.2_ august 11, 2013\n"; request request = new request.builder() .url("https://api.github.com/markdown/raw") .post(requestbody.create(media_type_markdown, postbody)) .build(); response response = client.newcall(request).execute(); if (!response.issuccessful()) throw new ioexception("unexpected code " + response); system.out.println(response.body().string());
this snippet taken from: https://github.com/square/okhttp/wiki/recipes
Comments
Post a Comment