Showing posts with label httpclient. Show all posts
Showing posts with label httpclient. Show all posts

Thursday, April 18, 2013

Java - sending HTTP parameters via POST method Using HttpClient


HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://something.com/login");
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("username", "username"));
list.add(new BasicNameValuePair("password", "password"));
httppost.setEntity(new UrlEncodedFormEntity(list));

HttpResponse response = client.execute(httppost);
System.out.println("STATUS LINE: "+response.getStatusLine());
System.out.println("STATUS PHRASE: "+response.getStatusLine().getReasonPhrase());
System.out.println("STATUS METHOD: "+httppost.getMethod());
HttpEntity entity = response.getEntity();

if (entity != null) {
    System.out.println("CONTENT:\n");    
    InputStream inputStream = entity.getContent();
    BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println("LINE: "+line);
    }
    br.close();
}