Skip to content

Instantly share code, notes, and snippets.

@KomanRudden
Last active August 29, 2015 13:56
Show Gist options
  • Save KomanRudden/8901329 to your computer and use it in GitHub Desktop.
Save KomanRudden/8901329 to your computer and use it in GitHub Desktop.
JAX-RS 1.0 Client examples - Apache HTTPClient
package i.am.koman.wsclients;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ApacheHTTP_Client {
public static void main(String args[]) throws Exception {
callApacheHTTPClient();
}
private static void callApacheHTTPClient() throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://<IP Address>:8080/<path-to-rest-service>/go");
request.addHeader("accept", MediaType.TEXT_PLAIN);
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() != 200) {
System.out.println("BIG APACHE HTTPCLIENT ERROR");
} else {
System.out.println("APACHE HTTPCLIENT HAPPY DAYS");
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String output;
while ((output = br.readLine()) != null) {
System.out.println(output);
}
}
client.getConnectionManager().shutdown();
}
}
//MAVEN dependencies
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.1.1</version>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment