Menu

Netlib HttpUtil

Tobias Boese

Netlib HttpUtil

Abstract: This class HttpUtil supports simple HTTP communication top of a silvertunnel-ng.org Netlib NetLayer.

The class org.silvertunnel_ng.netlib.util.HttpUtil contains different methods to execute a HTTP GET or POST request. All of these methods return a byte array as result which represents the HTTP response body. HttpUtil does not support reading or writing any HTTP header information, i.e. it is really a very simple way of communication.

Hint: HttpUtil only supports client-side HTTP.

HTTP GET Example

It follows an example:

  • select the NetLayer implementation that should be used:

        // classic:   TcpipNetLayer with NetLayerIDs.TCPIP (--> HTTP over plain TCP/IP);
    // anonymous: TorNetLayer with NetLayerIDs.TOR (--> HTTP over TCP/IP over Tor network):
    NetLayer lowerNetLayer = NetFactory.getInstance().getNetLayerById(NetLayerIDs.TOR);

  • only if anonymous communication over Tor is selected: it makes sense to wait until the Tor startup is finished - optional:

        // wait until TOR is ready (optional):
    lowerNetLayer.waitUntilReady();

  • send HTTP GET request to URL http://httptest.silvertunnel.org:80/httptest/bigtest.jsp?id=example and receive the response:

        // prepare parameters
    TcpipNetAddress httpServerNetAddress = new TcpipNetAddress("httptest.silvertunnel-ng.org", 80);
    String pathOnHttpServer = "/httptest/bigtest.jsp?id=example";
    long timeoutInMs = 5000;

    // do the request and wait for the response
    byte[] responseBody = HttpUtil.get(lowerNetLayer, httpServerNetAddress, pathOnHttpServer, timeoutInMs);
    
    // print out the response
    System.out.println(new String(responseBody));
    

More


Top: [Netlib], Next: [Netlib HTTP Client for Apache API]


Related

Wiki: Netlib HTTP Client for Apache API
Wiki: Netlib HTTP
Wiki: Netlib NetLayer
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.