hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ander Juaristi Alamos" <ajuari...@gmx.es>
Subject Simple non-blocking HTTP request with HttpCore
Date Wed, 03 Dec 2014 18:18:32 GMT
Hi all,

I'm trying to send a non-blocking (beware: non-blocking != asynchronous) HTTP request, and
based on the documentation and some source code lookup, I've written the following code:

  BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("GET", "/");
  BasicHttpEntity entity = new BasicHttpEntity();

  InetSocketAddress addr = new InetSocketAddress("www.****.es", 80);
  SocketChannel socket = SocketChannel.open(addr);
  SelectionKey sk = socket.register(Selector.open(), socket.validOps(), null);

  IOSessionImpl ioSess = new IOSessionImpl(sk, mySessionClosedCallback);
  DefaultNHttpClientConnection nbConn =
    new DefaultNHttpClientConnection(ioSess, 8*1024);



Unfortunately, it doesn't work as expected. It gets blocked forever in the while loop, because
nbConn returns always false. What's more, I can't see any actual data being sent from my computer
after the call to submitRequest(). The only traffic I see is the TCP connection being established
when I call SocketChannel.open().

What I want to do is to establish a non-blocking connection, send an HTTP request, let HttpCore
do the decoding work (so I don't have to write an HTTP chunk decoder myself) for me and then
close the socket.
I don't want to use the IO dispatcher, since I've observed a separate thread must be created
solely for it, and I don't want that, because my threads are state machines that do different
things. For now, I'll stick to that model.

In short, instead of letting HttpCore signal me when an event happens, I'd rather poll HttpCore
periodically to see if anything has happened and if it has, get the data decoded. If it hasn't,
I'll do other things and poll again later. Is this possible with HttpCore, or not? Or maybe
should I use HttpClient instead?

Thank you for your time and answers.

- AJ

To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org

View raw message