3

Currently I am using Apache http components client V4.3.5. In my case, I can upload small file(1kb), but it is not working on large file(100kb) when I run the code and get the exception "org.apache.http.NoHttpResponseException: 192.168.128.109:443 failed to respond". Can anyone take a look at my code and let me know what causes my issue?

Here is my code:

public static void main(String[] args) throws IOException,
        KeyStoreException {
    try {
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(
                null, new TrustStrategy() {
                    public boolean isTrusted(X509Certificate[] chain,
                            String authType) throws CertificateException {
                        return true;
                    }
                }).build();

        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslContext,
                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpClientBuilder builder = HttpClientBuilder.create();
        builder.disableContentCompression();
        builder.setSSLSocketFactory(sslsf);
        SocketConfig config = SocketConfig.custom().setSoKeepAlive(true).setSoTimeout(300000).build();
        builder.setDefaultSocketConfig(config);
        CloseableHttpClient httpclient = builder.build();

        HttpPost httppost = new HttpPost("https://192.168.128.109/upload");

        String encodedAuthorization = DatatypeConverter
                .printBase64Binary("admin:itronitr".getBytes());
        httppost.setHeader("Authorization", "Basic " + encodedAuthorization);

        FileBody bin = new FileBody(new File("c:\\test.txt"));
        String boundary = "hK1oPL5_XSfbm6lkCNlKI63rltrew5Bqik0ul";

        HttpEntity reqEntity = MultipartEntityBuilder.create()
                .setBoundary(boundary).addPart("upfile", bin).build();

        httppost.setEntity(reqEntity);

        System.out.println(httppost.getEntity().getContentLength());
        System.out
                .println(httppost.getEntity().getContentType().toString());

        CloseableHttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();
        System.out.println(response.getStatusLine());

        String content = EntityUtils.toString(resEntity);
        System.out.println(content);

    } catch (Exception exc) {
        exc.printStackTrace();
    }

}

Thanks, Bill

biao li
  • 111
  • 1
  • 1
  • 6
  • We use python to upload the same file to the server and it is working. This issue only happens in Java. My JDK is java version "1.7.0_67". – biao li Oct 01 '14 at 00:19
  • I fix it by overriding the HttpRequestRetryHandler in HTTP client. http://stackoverflow.com/questions/10570672/get-nohttpresponseexception-for-load-testing/10680629#10680629 – lzj micheal Sep 22 '16 at 06:30

3 Answers3

2

Finally I fix the issue and it is caused by buffer size. By default, buffer size of httpclient is 8k. So I change it to 4k and my code works well.

Here is the code that changes buffer size:

 ConnectionConfig connectionConfig = ConnectionConfig.custom()
                .setBufferSize(4128)
                .build();

 CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultConnectionConfig(connectionConfig)
                .build();
biao li
  • 111
  • 1
  • 1
  • 6
2

This is what worked for me; may or may not work for you!!

I recently encountered the same issue and tried all the suggestions whatever I was able to find on internet i.e upgrading httpClient to latest version and adding a re-try handler ; but none fixed it for me.

I already had a re-try handler built in my code and was running on the latest Apache client, but it was still failing with the exception Caused by: org.apache.http.NoHttpResponseException: xxxxx:443 failed to respond So, took me almost 2 days to debug this issue and find the root cause (at-least in my case)

There seems to be a bug in older Java versions up to Java 11.0.3 included that prevents Apache HTTP Client from sending payloads bigger than 16368 bytes caused by https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8214339.

I was running on java 11.0.2 and when I upgraded to 11.0.10, it worked for me and I was able to send the bigger payload without any code changes

ajain
  • 444
  • 4
  • 7
1

I also faced the similar problem. I went through many blogs and forums and tried various things but none worked for me. So, I tried a workaround. I added retry handler as below. And it worked for me:

HttpClientBuilder.create()
            .setDefaultCredentialsProvider(provider)
            .setRetryHandler(new DefaultHttpRequestRetryHandler() {
                @Override
                public boolean retryRequest(final IOException exception, final int executionCount, final HttpContext context) {
                    if (exception instanceof NoHttpResponseException) {
                        return true;
                    }
                    return super.retryRequest(exception, executionCount, context);
                }
            })
            .build();

Although it is not a correct fix and just a workaround but it is working for me for now. I'll stick to this solution till I won't get any permanent solution. Sharing it here in case someone might get benefit from it.

Nitin Singhal
  • 591
  • 5
  • 6