Skip to content
JakeHoward edited this page Jul 16, 2015 · 1 revision

Let's take a look at a sample application using Client API from Utterlyidle:

import com.googlecode.utterlyidle.HttpHandler;
import com.googlecode.utterlyidle.Request;
import com.googlecode.utterlyidle.Response;
import com.googlecode.utterlyidle.handlers.ClientHttpHandler;

import static com.googlecode.totallylazy.Uri.uri;
import static com.googlecode.utterlyidle.RequestBuilder.get;

public class ClientAPITest {
    public static void main(String[] args) throws Exception {
        HttpHandler httpClient = new ClientHttpHandler(2000);
        Request request = get(uri("http://code.google.com/p/utterlyidle/")).build();
        Response response = httpClient.handle(request);
        System.out.println(response);
    }
}
  1. First, we created ClientHttpHandler and we passed a timeout value (2000 ms) to the constructor.

  2. Then we used a RequestBuilder to create a request object.

  3. We sent a request to the server (http://code.google.com/p/utterlyidle/)

  4. We printed out the response. Utterlyidle gives us a nice representation of the response with all HTTP headers and a response body.

Please note that a client class (com.googlecode.utterlyidle.handlers.!ClientHttpHandler) implements the same interface as the server API (com.googlecode.utterlyidle.Application). We think that there's no reason to create distinct client and server APIs.

public interface HttpHandler {
    Response handle(final Request request) throws Exception;
}

Using this one method you can issue all kinds of requests (GET, POST, PUT, DELETE). Differences between different requests are encapsulated in a Request, so there's no need to have separate methods per HTTP verb in the client HTTP handler.

Proxy setup

Most of the time, the auto configuration should work

   HttpHandler httpClient = new ClientHttpHandler(2000,  Proxies.autodetectProxies());

If you want to set a proxy manually for all destination Urls, use the following

   Some<Proxy> proxy = new ProxyFor() {
      public Option<Proxy> proxyFor(Uri uri) {
         return some(new Proxy(HTTP, new InetSocketAddress("my.proxy.com", 8080)))
      }
   };
   HttpHandler httpClient = new ClientHttpHandler(2000,  proxy);

TODO

  • caching
Clone this wiki locally