Skip to content
A Java library that simplifies the process of sending HTTP requests and handling their responses
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
HTTPicnic/src/org/henrya/httpicnic
docs
examples
CODE_OF_CONDUCT.md
LICENSE
README.md

README.md

HTTPicnic

HTTPicnic is a Java library that greatly simplifies the process of sending HTTP requests and handling their responses.

View the Javadocs here

Maven

<dependency>
  <groupId>org.henrya</groupId>
  <artifactId>httpicnic</artifactId>
  <version>1.1.0</version>
</dependency>

HttpRequest

The HttpRequest class is an abstract class that is used to send basic HTTP requests.

When creating a new request, the URL that the request will be sent to is defined in the constructor.

HttpRequest request = new HttpPost("https://example.com/login");

Headers and parameters can then added to the request. An HttpConnectionException will be thrown if there is a problem connecting to the server.

HttpRequest request = new HttpRequest("http://example.com/login");
request.addHeader(HttpHeader.ACCEPT, "text/html");
request.addParameter("username", "henry");
request.addParameter("password", "password123");
HttpResponse response = request.sendPOST();
System.out.println(response.getContent();

The HttpResponse object contains all the data that the web server replied with.

PicnicClient

The PicnicClient class is used to send multiple HTTP requests. It handles the cookies from each request and is useful in cases where cookies need to be stored and sent in every request.

PicnicClient client = new PicnicClient();
client.addParameter("username", "henry");
client.addParameter("password", "password123");
client.sendPOST("http://example.com/login");
HttpResponse response = client.sendGET("http://example.com/control_panel");
System.out.println(response.getContent());

In this scenario the goal would be to access a control panel that requires the user to be logged in to view. To accomplish this we would send a POST request to the login page which would respond with a session cookie. The PicnicClient class handles the session cookie and sends it with the next GET request to the user's control panel to show the server that the user is logged in.

You can’t perform that action at this time.