Skip to content

Instantly share code, notes, and snippets.

@Xennis
Last active November 11, 2016 13:38
Show Gist options
  • Save Xennis/9263251 to your computer and use it in GitHub Desktop.
Save Xennis/9263251 to your computer and use it in GitHub Desktop.
Java HTTP Client (httpGet and httpPost with OAuth)

Java HTTP Client (httpGet, httpPost)

Full documentation see: XennisWiki - Java - JSON and REST

Used software

Used Java libraries

Run

  1. Included the necessary JARs in the Java build path

    • httpcomponents-client/lib/
      • commons-logging-1.1.3.jar
      • httpclient-4.3.2.jar
      • httpcore-4.3.1.jar
  2. Compile all Java classes

Usage

Get JSON example with OAuth

String result = ApacheHttpClient.httpGet("http://example.org/api/message/", "application/json", "OAuth " + ACCESS_TOKEN);

Post JSON example wit OAuth

import org.apache.http.entity.ContentType;
// ...
boolean result = ApacheHttpClient.httpPost("http://example.org/api/message/", "OAuth " + ACCESS_TOKEN, stringEntity, ContentType.APPLICATION_JSON);
package org.example.sampleapi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
/**
* This class is used for GET and POST communication wit an Apache HTTP
* server.
*
* It requires:
* Apache HttpComponents "HttpClient" - http://hc.apache.org/downloads.cgi
*/
public class ApacheHttpClient {
/**
* Do a HTTP GET request and return the result.
*
* @param uri
* URI for HTTP GET request
* @param headerAccept
* Empty or header accept
* @param headerAuthorization
* Empty or authorization header
* @return Response (content of the received entity)
* @throws RuntimeException
* When status code is not HTTP_OK
*/
public static String httpGet(String uri, String headerAccept, String headerAuthorization) throws RuntimeException {
StringBuilder result = new StringBuilder();
HttpClient httpClient = HttpClientBuilder.create().build();
try {
HttpGet getRequest = new HttpGet(uri);
if (!headerAccept.isEmpty()) {
getRequest.addHeader(HttpHeaders.ACCEPT, headerAccept);
}
if (!headerAuthorization.isEmpty()) {
getRequest.addHeader(HttpHeaders.AUTHORIZATION, headerAuthorization);
}
HttpResponse response = httpClient.execute(getRequest);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("ERROR: HTTP code: " + statusCode);
}
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String line = "";
while((line = br.readLine()) != null){
result.append(line);
}
br.close();
} catch (ConnectException e) {
System.err.println("ConnectException: " + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
return result.toString();
}
/**
* Do a HTTT POST request and return the status code.
*
* @param uri
* URI for HTTP POST request
* @param headerAuthorization
* Empty or authorization header
* @param stringEntity
* String entity which will be posted
* @param entityConentType
* Empty or entity content type
* @return True, when request was successful
* @throws RuntimeException
* When status code is not HTTP_CREATED
*/
public static boolean httpPost(String uri, String headerAuthorization, String stringEntity, ContentType entityConentType) throws RuntimeException {
HttpClient httpClient = HttpClientBuilder.create().build();
try {
HttpPost postRequest = new HttpPost(uri);
if (!headerAuthorization.isEmpty()) {
postRequest.addHeader(HttpHeaders.AUTHORIZATION, headerAuthorization);
}
StringEntity ent = new StringEntity(stringEntity);
if (entityConentType != null) {
ent.setContentType(entityConentType.toString());
}
postRequest.setEntity(ent);
HttpResponse response = httpClient.execute(postRequest);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("ERROR: HTTP code: " + statusCode);
}
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment