org.apache.http.NoHttpResponseException:XX.XX.XX.XX:443无法响应 [英] org.apache.http.NoHttpResponseException: XX.XX.XX.XX:443 failed to respond

查看:118
本文介绍了org.apache.http.NoHttpResponseException:XX.XX.XX.XX:443无法响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用Apache http组件客户端V4.3.5.就我而言,我可以上传小文件(1kb),但是当我运行代码并获得异常"org.apache.http.NoHttpResponseException:192.168.128.109:443无法响应时,它不能在大文件(100kb)上运行" ".谁能看看我的代码,让我知道是什么原因导致我的问题?

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?

这是我的代码:

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();
    }

}

谢谢, 比尔

推荐答案

最后,我解决了该问题,这是由缓冲区大小引起的.默认情况下,httpclient的缓冲区大小为8k.所以我将其更改为4k,我的代码运行良好.

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();

这篇关于org.apache.http.NoHttpResponseException:XX.XX.XX.XX:443无法响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆