Menu

Netlib HTTP Client for Apache API

Tobias Boese

Netlib HTTP Client for Apache API

Abstract: Netlib HTTP Client for Apache API - development is currently interrupted.

IMPLEMENTATION STARTED BUT INTERRUPTED (package: org.silvertunnel.netlib.adapter.httpclient)

Outdated Information - please ignore

Intro

Change of the TCP/IP Socket Implementation in HttpClient 4.0

Use of the HttpClient 4.0:

Several examples are available from the HttpClient project: trunk/httpclient/src/examples/org/apache/http/examples/client/.

This is an modified examples of the HttpClient 4.0 use:

        // the request URL
        final HttpHost requestedHost = new HttpHost("issues.apache.org", 80, "http");
        HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);

        // Register the "http" protocol scheme, it is required
        // by the default operator to look up socket factories.
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        SocketFactory sf = MySocketFactory.getSocketFactory();
        final int PORT_80 = 80;
        schemeRegistry.register(new Scheme("http", sf, PORT_80));

        // prepare parameters
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, "UTF-8");

        // create http client
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, schemeRegistry);
        HttpClient httpClient = new DefaultHttpClient(ccm, params);

        // send request and receive response
        HttpResponse response = httpClient.execute(requestedHost, request);

        // read the response
        HttpEntity responseEntity = response.getEntity();
        Header[] headers = response.getAllHeaders();
        System.out.println(EntityUtils.toString(responseEntity));

Our own socket implementation will be encapsuleted in an own SocketFactory implementation called MySocketFactory.

/**
  * A own implementation of a SocketFactory.
  *
  * HttpClients usually uses PlainSocketFactory.
  */
public class MySocketFactory implements SocketFactory {
    public Socket createSocket() throws IOException {
        // ...
    }

    public Socket connectSocket(
        Socket sock,
        String host,
        int port,
        InetAddress localAddress,
        int localPort,
        HttpParams params
    ) throws IOException, UnknownHostException, ConnectTimeoutException {
        // ...
    }

    public boolean isSecure(Socket sock) throws IllegalArgumentException {
        // ...
    }
}

Top: [Netlib], Next: [Netlib HTTP on High Latency Connections API]


Related

Wiki: Netlib Adapters
Wiki: Netlib HTTP on High Latency Connections API
Wiki: Netlib HTTP
Wiki: Netlib HttpUtil
Wiki: Netlib

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.