1

I am attempting to use Apache's HttpClient 4.5.2. I am running on a Wildfly 10 server. I really would like to use org.apache.http.client.fluent.Request. The call works the first time. Here is what the code looks like

InputStream stream = null;
try {
    Request httpGetRequest = Request.get(queryUrl);
    httpGetRequest.viaProxy(new HttpHost(httpsProxyHost, httpProxyPort.intValue()));

    HttpResponse response = httpGetRequest.execute().returnResponse();

    stream = response.getEntity().getContent();
    //Process Stream....
} catch (Exception e) {
    logger.error("Nope!.   Try again", e);
} finally {
    try {
        if (stream != null) {
            stream.close();
        }
    } catch (Exception e) {
        logger.warn("Problem closing stream!", e);
    }
}

Works the first time.. but the second time I get a Connection Timeout error..

Important to note: If not going thru the Proxy and Not using SSL (different url, but same code) it works everytime.

Any ideas?

thanks.

wrgoff
  • 21
  • 3
  • Not an answer, but have you considered using the [javax.ws.rs.client.Client](http://docs.oracle.com/javaee/7/api/javax/ws/rs/client/Client.html) API for doing this? – Steve C Aug 18 '16 at 14:35

1 Answers1

0

I feel that you're closing things incorrectly. I do it a bit differently but I'll admit that I only use SSL, not a proxy. My code looks something like:

try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
    HttpGet httpGet = new HttpGet(url);

    try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
        InputStream stream = response.getEntity().getContent();

        // read as needed
    }
}

In this way I'm making sure that the HttpClient class are closed, not an internal InputStream. Note that this code uses the Closable interface so you'll have to be using Java 1.5 and above.

stdunbar
  • 16,263
  • 11
  • 31
  • 53
  • You probably need to read up on InputStream (and Streams in general). Some of the issues that NOT closing an InputStream can cause is file handlers left open.. Never to be reclaimed (until the JVM is restart). You really should be closing anything with a close method. Also keep in mind you are using a different API.. I am specifically asking about "org.apache.http.client.fluent.Request". But just FYI, the same thing happens with HttpClient.. Just a lot more code on my end. – wrgoff Aug 22 '16 at 09:30
  • That's why I'm using the Closable interface - it is closed for me at the end of the statement automatically by the compiler - no need for the finally or an explicit close. Perhaps you should read up on try with resources :) Sorry if I misread the question initially. – stdunbar Aug 22 '16 at 14:17
  • I understand that, and I know it closes it (in this case the httpClient and response, but not the stream) for you, this came out with Java 1.7., But you are still leaving a handle to InputStream created from the Reponse open. Read this.. http://stackoverflow.com/questions/22889075/do-unclosed-streams-cause-memory-leaks-in-java. It is always go practice to close any object that has a close method... Why else do you suppose it is there? – wrgoff Aug 22 '16 at 16:22
  • In my example above, Request (httpGetRequest ) and HttpResponse (response) are from the org.apache.http.client.fluent package. And are supposed to handle the closing for you (and appear to). The only thing left to worry about is supposed to be the InputStream created from the content. But as I said above, it works the first time, but not the second (only when using a proxy/ssl). It works everytime (for a separate call) when I am not using a proxy or ssl. – wrgoff Aug 22 '16 at 16:33
  • I don't feel that I am - notice this is a try with resources inside of a try with resources. I never close something I haven't opened and, indeed, all resource that I opened have been closed. If you use System.out for printing do you close that? I guess I don't because I didn't open it. And I'm not too concerned about using a JDK released 5 years ago. But we're off in the weeds - again, I don't think this is where you want to go and sorry for misreading this initially, – stdunbar Aug 22 '16 at 16:33
  • try reading this.. http://stackoverflow.com/questions/4767553/safe-use-of-httpurlconnection This is basically the same thing.. Just using HttpURLConnection (Which I have working for my case) instead of HttpClient. You called something that gave you back a resource (InputStream). If you called something that gave you a resource, it is up to you to close the handle when done. – wrgoff Aug 22 '16 at 16:46
  • I have been doing Java for soo long (1.1), that the resource inside a try declaration seems strange to me.. I guess I will get used to it, eventually ;-) – wrgoff Aug 22 '16 at 16:48
  • 1
    Found the answer.. In my hast, my "httpsProxyHost" and "httpsProxyPort" were not defined as static. So first time in they were getting set, and next time in they were null.. Surprised the call didn't give me a null pointer exception on the Proxy. That what I get for being in a hurry. – wrgoff Aug 22 '16 at 18:05