A simple convenience library for using a HttpURLConnection to make requests and access the response.
The http-request library is available from Maven Central.
<dependency>
<groupId>com.github.kevinsawicki</groupId>
<artifactId>http-request</artifactId>
<version>5.4.1</version>
</dependency>Javadocs are available here.
See here for a list of known projects using this library.
This library was written to make HTTP requests simple and easy when using a HttpURLConnection.
Libraries like Apache HttpComponents are great but sometimes
for either simplicity, or perhaps for the environment you are deploying to (Android),
you just want to use a good old-fashioned HttpURLConnection. This library seeks
to add convenience and common patterns to the act of making HTTP requests such as
a fluid-interface for building requests and support for features such as multipart
requests.
Bottom line: The single goal of this library is to improve the usability of the
HttpURLConnection class.
None. The goal of this library is to be a single class class with some inner static classes. The test project does require Jetty in order to test requests against an actual HTTP server implementation.
The HttpRequest class does not throw any checked exceptions, instead all low-level
exceptions are wrapped up in a HttpRequestException which extends RuntimeException.
You can access the underlying exception by catching HttpRequestException and calling
getCause() which will always return the original IOException.
intresponse = HttpRequest.get("http://google.com").code();Stringresponse = HttpRequest.get("http://google.com").body();
System.out.println("Response was: " + response);HttpRequest.get("http://google.com").receive(System.out);HttpRequestrequest = HttpRequest.get("http://google.com", true, 'q', "baseball gloves", "size", 100);
System.out.println(request.toString()); // GET http://google.com?q=baseball%20gloves&size=100StringcontentType = HttpRequest.get("http://google.com")
.accept("application/json") //Sets request header
.contentType(); //Gets response headerSystem.out.println("Response content type was " + contentType);intresponse = HttpRequest.post("http://google.com").send("name=kevin").code();intresponse = HttpRequest.get("http://google.com").basic("username", "p4ssw0rd").code();HttpRequestrequest = HttpRequest.post("http://google.com");
request.part("status[body]", "Making a multipart request");
request.part("status[image]", newFile("/home/kevin/Pictures/ide.png"));
if (request.ok())
System.out.println("Status was updated");Map<String, String> data = newHashMap<String, String>();
data.put("user", "A User");
data.put("state", "CA");
if (HttpRequest.post("http://google.com").form(data).created())
System.out.println("User was created");Fileoutput = newFile("/output/request.out");
HttpRequest.get("http://google.com").receive(output);Fileinput = newFile("/input/data.txt");
intresponse = HttpRequest.post("http://google.com").send(input).code();Filelatest = newFile("/data/cache.json");
HttpRequestrequest = HttpRequest.get("http://google.com");
//Copy response to filerequest.receive(latest);
//Store eTag of responseStringeTag = request.eTag();
//Later on check if changes existbooleanunchanged = HttpRequest.get("http://google.com")
.ifNoneMatch(eTag)
.notModified();HttpRequestrequest = HttpRequest.get("http://google.com");
//Tell server to gzip response and automatically uncompressrequest.acceptGzipEncoding().uncompress(true);
Stringuncompressed = request.body();
System.out.println("Uncompressed response is: " + uncompressed);HttpRequestrequest = HttpRequest.get("https://google.com");
//Accept all certificatesrequest.trustAllCerts();
//Accept all hostnamesrequest.trustAllHosts();HttpRequestrequest = HttpRequest.get("https://google.com");
//Configure proxyrequest.useProxy("localhost", 8080);
//Optional proxy basic authenticationrequest.proxyBasic("username", "p4ssw0rd");intcode = HttpRequest.get("http://google.com").followRedirects(true).code();Looking to use this library with OkHttp? Read here.
HttpRequest.setConnectionFactory(newConnectionFactory() {
publicHttpURLConnectioncreate(URLurl) throwsIOException {
if (!"https".equals(url.getProtocol()))
thrownewIOException("Only secure requests are allowed");
return (HttpURLConnection) url.openConnection();
}
publicHttpURLConnectioncreate(URLurl, Proxyproxy) throwsIOException {
if (!"https".equals(url.getProtocol()))
thrownewIOException("Only secure requests are allowed");
return (HttpURLConnection) url.openConnection(proxy);
}
});