Java Post request with form data -
i want make simple post call in java,
getting 200 response code but, wrong response message,
told there different way make post call when using form data.
following current java code make post call -
private string makepostcall(){ try { string url = "http://someip/trusted"; httpclient client = new defaulthttpclient(); httppost post = new httppost(url); // add header list<namevaluepair> urlparameters = new arraylist<namevaluepair>(); urlparameters.add(new basicnamevaluepair("username", "app_user")); post.setentity(new urlencodedformentity(urlparameters)); httpresponse response = client.execute(post); system.out.println("\nsending 'post' request url : " + url); system.out.println("post parameters : " + post.getentity()); system.out.println("response code : " + response.getstatusline().getstatuscode()); bufferedreader rd = new bufferedreader(new inputstreamreader(response.getentity().getcontent())); stringbuffer result = new stringbuffer(); string line = ""; while ((line = rd.readline()) != null) { result.append(line); } system.out.println(result.tostring()); return result.tostring(); } catch (exception e) { e.printstacktrace(); return null; } }
following post call sample working through postman app -
i referring following website -
https://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/
the expected outcome of post call supposed token ie. string value, current response -1.
give try setting content type multipart/form-data
explicitly,
post.setheader("content-type", "multipart/form-data");
in code ,
post.setentity(new urlencodedformentity(urlparameters)); post.setheader("content-type", "multipart/form-data");
Comments
Post a Comment