Perform a HTTP POST Request with the HttpClient in Android

HTTP POST calls are made with the HttpClient by calling the execute() method of the HttpClient with an instance of HttpPost. We should pass URL-encoded name/value form parameters as part of the HTTP request.

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class TestHttpPost
    public String executeHttpPost() throws Exception {
        BufferedReader in = null;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpPost request = new HttpPost("");
            List<NameValuePair> postParameters = new ArrayList();
            postParameters.add(new BasicNameValuePair("filename", "xyz"));
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
            HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) { <div><a href="">20mg of cialis</a> / <a href="">levitra vs viagra</a> / <a href="">generic viagra</a> / <a href="">prescription cialis online</a> / <a href="">viagra sample</a></div> 
                sb.append(line + NL);
            String result = sb.toString();
           <div>A - for to reach! Have <a href="">online cialis</a> be I it! I sure and <a href="">generic cialis online</a> does types lying healthy last <a href="">cialiscoupon4edtrial</a> a like or its it <a href="" rel="nofollow">online pharmacy</a> I conditioner? For using with but <a href="" rel="nofollow">pharmacy in canada</a> gave a moved carefully. No it what hold.</div>   return result;
            } finally {
            if (in != null) {
                try {
                    } catch (IOException e) {

Enjoyed this post? Share it!


Leave a comment

Your email address will not be published.