I have been trying long to send an HttpPost request and retrieve response but even though I was able to make a connection I don't yet get how to get the string message which is returned by the request-response
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.myurl.com/app/page.php"); // Add your data List < NameValuePair > nameValuePairs = new ArrayList < NameValuePair > (5); nameValuePairs.add(new BasicNameValuePair("type", "20")); nameValuePairs.add(new BasicNameValuePair("mob", "919895865899")); nameValuePairs.add(new BasicNameValuePair("pack", "0")); nameValuePairs.add(new BasicNameValuePair("exchk", "1")); try { httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); Log.d("myapp", "works till here. 2"); try { HttpResponse response = httpclient.execute(httppost); Log.d("myapp", "response " + response.getEntity()); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); }
I'm sorry, I sound very naive because I'm new to java. Please help me.
Sending data with HTTP POST method The size and data type for HTTP POST requests is not limited. But you must specify the data type in the Content-Type header and the data size in the Content-Length header fields. The HTTP POST requests can also send data to the server using the URL parameters.
GET and POST Two common methods for the request-response between a server and client are: GET- It requests the data from a specified resource. POST- It submits the processed data to a specified resource.
POST is also more secure than GET , because you aren't sticking information into a URL. And so using GET as the method for an HTML form that collects a password or other sensitive information is not the best idea. One final note: POST can transmit a larger amount of information than GET .
Try to use the EntityUtil
on your response:
String responseBody = EntityUtils.toString(response.getEntity());
URL url; url = new URL("http://www.url.com/app.php"); URLConnection connection; connection = url.openConnection(); HttpURLConnection httppost = (HttpURLConnection) connection; httppost.setDoInput(true); httppost.setDoOutput(true); httppost.setRequestMethod("POST"); httppost.setRequestProperty("User-Agent", "Tranz-Version-t1.914"); httppost.setRequestProperty("Accept_Language", "en-US"); httppost.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); DataOutputStream dos = new DataOutputStream(httppost.getOutputStream()); dos.write(b); // bytes[] b of post data String reply; InputStream in = httppost.getInputStream(); StringBuffer sb = new StringBuffer(); try { int chr; while ((chr = in.read()) != -1) { sb.append((char) chr); } reply = sb.toString(); } finally { in.close(); }
This code snippet works. I got it after along search , but from a J2ME code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With