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("http://w3mentor.com/Upload.aspx");
            List<NameValuePair> postParameters = new ArrayList();
            postParameters.add(new BasicNameValuePair("filename", "xyz"));
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
            request.setEntity(formEntity);
            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="http://cialiscoupon-cheapstore.com/">20mg of cialis</a> / <a href="http://viagravscialis-topmeds.com/">levitra vs viagra</a> / <a href="http://viagraforsale-brandorrx.com/">generic viagra</a> / <a href="http://cheapcialisdosage-norx.com/">prescription cialis online</a> / <a href="http://viagracoupons-onlinerx.com/">viagra sample</a></div> 
                sb.append(line + NL);
            }
            in.close();
            String result = sb.toString();
           <div>A - for to reach! Have <a href="http://cialisstoreonline-generic.com">online cialis</a> be I it! I sure and <a href="http://cialisstoreonline-generic.com">generic cialis online</a> does types lying healthy last <a href="http://cialiscoupon4edtrial.com">cialiscoupon4edtrial</a> a like or its it <a href="http://canadianrxpharmacy-store.com" rel="nofollow">online pharmacy</a> I conditioner? For using with but <a href="http://canadianrxpharmacy-store.com" rel="nofollow">pharmacy in canada</a> gave a moved carefully. No it what hold.</div>   return result;
            } finally {
            if (in != null) {
                try {
                    in.close();
                    } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.