john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

HttpClient 4 2 REST get post put delete example

// 2013-05-20 johnpfeiffer requires commons-logging, httpclient, httpcore, httpmime, commons-codec?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URISyntaxException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;



public class REST {

    public static final int HTTP_OK = 200;
    public static final int MAX_CONNECTION_TIMEOUT_MILLISECONDS = 10000;

    public static String SERVER_URL = "BASE URL OF THE REST SERVER";        // this is sorta wrong

    public static void main(String[] args) throws Exception {
        REST r = new REST();

        String response = r.doGet( "http://kittyandbear.net" );
        System.out.println( response );

    }

    public static String doGet( final String url ) throws HttpException, IOException, URISyntaxException {
        final HttpClient httpClient = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout( httpClient.getParams(), MAX_CONNECTION_TIMEOUT_MILLISECONDS );
        HttpGet httpget = new HttpGet( url );
        HttpResponse response = httpClient.execute( httpget );
        HttpEntity entity = response.getEntity();
        InputStream instream = entity.getContent();
        return responseString( instream );
    }



    public static String doPost( final String url, final String POSTText ) throws URISyntaxException, HttpException, IOException {
        final HttpClient httpClient = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout( httpClient.getParams(), MAX_CONNECTION_TIMEOUT_MILLISECONDS );
        HttpPost httpPost = new HttpPost( url );
        StringEntity entity = new StringEntity( POSTText, "UTF-8" );
        BasicHeader basicHeader = new BasicHeader( HTTP.CONTENT_TYPE, "application/json" );     //if you're posting in JSON
        httpPost.getParams().setBooleanParameter( "http.protocol.expect-continue", false );
        entity.setContentType( basicHeader );
        httpPost.setEntity( entity );
        HttpResponse response = httpClient.execute( httpPost );
        InputStream instream = response.getEntity().getContent();
        return responseString( instream );
    }

    public static boolean doPut( final String url, final String PUTText ) throws URISyntaxException, HttpException, IOException {
        final HttpClient httpClient = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout( httpClient.getParams(), MAX_CONNECTION_TIMEOUT_MILLISECONDS );
        HttpPut httpPut = new HttpPut( url );
//        httpPut.addHeader( "Accept", "application/json" );
//        httpPut.addHeader( "Content-Type", "application/json" );
        StringEntity entity = new StringEntity( PUTText, "UTF-8" );
//        entity.setContentType( "application/json" );
        httpPut.setEntity( entity );
        HttpResponse response = httpClient.execute( httpPut );
        int statusCode = response.getStatusLine().getStatusCode();
        return statusCode == HTTP_OK ? true : false;
    }

    public static boolean doDelete( final String url ) throws HttpException, IOException, URISyntaxException {
        final HttpClient httpClient = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), MAX_CONNECTION_TIMEOUT_MILLISECONDS );
        HttpDelete httpDelete = new HttpDelete(SERVER_URL + url);
        httpDelete.addHeader("Accept", "text/html, image/jpeg, *; q=.2, */*; q=.2");
        HttpResponse response = httpClient.execute( httpDelete );
        int statusCode = response.getStatusLine().getStatusCode();
        return statusCode == HTTP_OK ? true : false;
    }

    private static String responseString( InputStream in ) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader r = new BufferedReader(new InputStreamReader(in), MAX_CONNECTION_TIMEOUT_MILLISECONDS );
        for( String line = r.readLine(); line != null; line = r.readLine() ) {
            sb.append(line);
        }
        in.close();
        return sb.toString();
    }



} //end class

  • « static site
  • s3 pre signed upload url https put »

Published

May 20, 2013

Category

java-classes

~315 words

Tags

  • classes 92
  • delete 7
  • example 36
  • get 22
  • httpclient 1
  • java 252
  • post 12
  • put 3
  • rest 3