Discussion:
HttpClient 5
Mark A. Claassen
2018-04-04 21:01:18 UTC
Permalink
I am finally getting chance to start with HttpClient 5 and HTTP/2. I am currently using 4.4.

I am first trying to do a relatively simple port, meaning that I am trying to keep as much of the existing code as I can. I already have an internal API that reads the entity objects from the CloseableHttpResponse, so I am trying to get something that looks as close to that as possible.

Looking at the examples, I think I need to use the Async client for HTTP/2. I created a CloseableHttpAsyncClient which I can call execute on and get a SimpleHttpResponse. However, this object doesn't give me anything I can easily convert to an input stream, just things like getText() and getBytes(). Getting an input stream is pretty fundamental to my internal API, so not being able to get that is going to make my job quite a bit more difficult.

If I use a different call to execute() and get a ResponseConsumer, it looks like I can do more stream like things, but seems like a lot for the little I want.

I certainly still need to poor over the examples ore and try to figure all this out. However, a nudge in the right direction would be extremely appreciated.

Thanks!

Mark Claassen
Senior Software Engineer

Donnell Systems, Inc.
130 South Main Street
Leighton Plaza Suite 375
South Bend, IN 46601
E-mail: mailto:***@ocie.net
Voice: (574)232-3784
Fax: (574)232-4014

Disclaimer:
The opinions provided herein do not necessarily state or reflect
those of Donnell Systems, Inc.(DSI). DSI makes no warranty for and
assumes no legal liability or responsibility for the posting.
Oleg Kalnichevski
2018-04-05 06:44:35 UTC
Permalink
I am finally getting chance to start with HttpClient 5 and HTTP/2.  I
am currently using 4.4.
I am first trying to do a relatively simple port, meaning that I am
trying to keep as much of the existing code as I can.  I already have
an internal API that reads the entity objects from the
CloseableHttpResponse, so I am trying to get something that looks as
close to that as possible.
Looking at the examples, I think I need to use the Async client for
HTTP/2.  I created a CloseableHttpAsyncClient which I can call
execute on and get a SimpleHttpResponse.  However, this object
doesn't give me anything I can easily convert to an input stream,
just things like getText() and getBytes().  Getting an input stream
is pretty fundamental to my internal API, so not being able to get
that is going to make my job quite a bit more difficult.
If I use a different call to execute() and get a ResponseConsumer, it
looks like I can do more stream like things, but seems like a lot for
the little I want.
I certainly still need to poor over the examples ore and try to
figure all this out.  However, a nudge in the right direction would
be extremely appreciated.
Thanks!
Hi Mark

Classic client APIs in HttpClient 5.0 will give you very similar,
mostly compatible APIs with HttpClient 4.x but no HTTP/2.

If you want to utilize HTTP/2 you should be using async APIs provided
by HttpClient 5.0. HttpAsyncClient is perfectly capable of streaming
both request and response data (in full-duplex if desired) but you
would lose convenience of InputStream/OutputStream APIs and would need
to structure your application logic in terms of callback methods
reacting to various events during HTTP message exchanges: 

https://github.com/apache/httpcomponents-client/blob/master/httpclient5
/src/examples/org/apache/hc/client5/http/examples/AsyncClientHttpExchan
geStreaming.java

https://github.com/apache/httpcomponents-client/blob/master/httpclient5
/src/examples/org/apache/hc/client5/http/examples/AsyncClientFullDuplex
Exchange.java

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-***@hc.apache.org
For additional commands, e-mail: httpclient-users-***@hc.apache.org
Mark A. Claassen
2018-04-05 14:25:46 UTC
Permalink
Thanks, that was what I was looking for. However, a few things...
First, it seems the arguments have changed a bit since that was written.

I fixed this by creating a HttpContext with : HttpContext context = HttpClientContext.create();
I then created a quick CallbackHandler for testing:
FutureCallback<MyHandler> callback = new FutureCallback<MyHandler>() {
@Override
public void completed(MyHandler t) {
System.err.println("Here: completed");
}
@Override
public void failed(Exception excptn) {
System.err.println("Here: failed");
excptn.printStackTrace();
}
@Override
public void cancelled() {
System.err.println("cancelled");
}
};

Second, I get an error. I think the problem is in MinimalHttpAsyncClient on line 229. It creates the HttpHost, but there is no opportunity to send in a port. This results in:

at org.apache.hc.core5.util.Args.notNegative(Args.java:124)
at org.apache.hc.client5.http.HttpRoute.<init>(HttpRoute.java:75)
at org.apache.hc.client5.http.HttpRoute.<init>(HttpRoute.java:154)
at org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient.leaseEndpoint(MinimalHttpAsyncClient.java:115)
at org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient.access$200(MinimalHttpAsyncClient.java:76)
at org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient$4.sendRequest(MinimalHttpAsyncClient.java:231)
at org.apache.hc.core5.http.nio.BasicRequestProducer.sendRequest(BasicRequestProducer.java:65)


Thanks,

Mark Claassen
Senior Software Engineer

Donnell Systems, Inc.
130 South Main Street
Leighton Plaza Suite 375
South Bend, IN  46601
E-mail: mailto:***@ocie.net
Voice: (574)232-3784
Fax: (574)232-4014

Disclaimer:
The opinions provided herein do not necessarily state or reflect
those of Donnell Systems, Inc.(DSI). DSI makes no warranty for and
assumes no legal liability or responsibility for the posting.

-----Original Message-----
From: Oleg Kalnichevski <***@apache.org>
Sent: Thursday, April 5, 2018 2:45 AM
To: HttpClient User Discussion <httpclient-***@hc.apache.org>
Subject: Re: HttpClient 5
I am finally getting chance to start with HttpClient 5 and HTTP/2.  I
am currently using 4.4.
I am first trying to do a relatively simple port, meaning that I am
trying to keep as much of the existing code as I can.  I already have
an internal API that reads the entity objects from the
CloseableHttpResponse, so I am trying to get something that looks as
close to that as possible.
Looking at the examples, I think I need to use the Async client for
HTTP/2.  I created a CloseableHttpAsyncClient which I can call execute
on and get a SimpleHttpResponse.  However, this object doesn't give me
anything I can easily convert to an input stream, just things like
getText() and getBytes().  Getting an input stream is pretty
fundamental to my internal API, so not being able to get that is going
to make my job quite a bit more difficult.
If I use a different call to execute() and get a ResponseConsumer, it
looks like I can do more stream like things, but seems like a lot for
the little I want.
I certainly still need to poor over the examples ore and try to figure
all this out.  However, a nudge in the right direction would be
extremely appreciated.
Thanks!
Hi Mark

Classic client APIs in HttpClient 5.0 will give you very similar, mostly compatible APIs with HttpClient 4.x but no HTTP/2.

If you want to utilize HTTP/2 you should be using async APIs provided by HttpClient 5.0. HttpAsyncClient is perfectly capable of streaming both request and response data (in full-duplex if desired) but you would lose convenience of InputStream/OutputStream APIs and would need to structure your application logic in terms of callback methods reacting to various events during HTTP message exchanges: 

https://github.com/apache/httpcomponents-client/blob/master/httpclient5
/src/examples/org/apache/hc/client5/http/examples/AsyncClientHttpExchan
geStreaming.java

https://github.com/apache/httpcomponents-client/blob/master/httpclient5
/src/examples/org/apache/hc/client5/http/examples/AsyncClientFullDuplex
Exchange.java

Oleg


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

B�KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKCB��[��X��ܚX�KK[XZ[��Y[� ]\�\��][��X��ܚX�P˘\X�K�ܙ�B��܈Y][ۘ[��[X[
Oleg Kalnichevski
2018-04-06 07:02:43 UTC
Permalink
Thanks, that was what I was looking for.  However, a few things...
First, it seems the arguments have changed a bit since that was written.
I fixed this by creating a HttpContext with : HttpContext context =
HttpClientContext.create();
FutureCallback<MyHandler> callback = new
FutureCallback<MyHandler>() {
@Override
public void completed(MyHandler t) {
System.err.println("Here: completed");
}
@Override
public void failed(Exception excptn) {
System.err.println("Here: failed");
excptn.printStackTrace();
}
@Override
public void cancelled() {
System.err.println("cancelled");
}
};
Second, I get an error.  I think the problem is in
MinimalHttpAsyncClient on line 229.  It creates the HttpHost, but
at org.apache.hc.core5.util.Args.notNegative(Args.java:124)
at
org.apache.hc.client5.http.HttpRoute.<init>(HttpRoute.java:75)
at
org.apache.hc.client5.http.HttpRoute.<init>(HttpRoute.java:154)
at
org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient.leaseEnd
point(MinimalHttpAsyncClient.java:115)
at
org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient.access$2
00(MinimalHttpAsyncClient.java:76)
at
org.apache.hc.client5.http.impl.async.MinimalHttpAsyncClient$4.sendRe
quest(MinimalHttpAsyncClient.java:231)
at
org.apache.hc.core5.http.nio.BasicRequestProducer.sendRequest(BasicRe
questProducer.java:65)
Hi Mark

Yes, things still keep on changing though I am trying to not make any
large API visible changes anymore.

Please do try the latest snapshot from master instead of 5.0-beta1

https://github.com/apache/httpcomponents-client

Oleg

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