httpclient - org.apache.http.NoHttpResponseException : XX. XX.XX.XX:443 响应失败

标签 httpclient

目前我使用的是Apache http components client V4.3.5。在我的例子中,我可以上传小文件(1kb),但是当我运行代码并得到异常“org.apache.http.NoHttpResponseException:192.168.128.109:443未能响应时,它无法处理大文件(100kb) ”。任何人都可以查看我的代码并让我知道是什么导致了我的问题吗?

这是我的代码:

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,我的代码运行良好。

这是更改缓冲区大小的代码:

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

 CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultConnectionConfig(connectionConfig)
                .build();

关于httpclient - org.apache.http.NoHttpResponseException : XX. XX.XX.XX:443 响应失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26111331/

相关文章:

java - 如何在 java 中创建安全的 AsyncHttpClient POST 请求

java - 如何使 Apache Commons HttpClient 3.1 忽略 HTTPS 证书无效?

wcf - 如何对使用 WCF Rest Starter Kit 构建的 WCF 客户端进行单元测试

java - 如何使用 HttpClient 在 Android 中管理 PHP session ?

Angular 5 : get method with params and observe response

android - Proguard 返回错误代码 1。请参阅控制台 java.io.IOException

java - 从 android httpclient 程序访问 Servlet 时连接被拒绝?

android - Httppost 找不到方法 setConfig android

使用 HttpClient 的 C# application/x-www-form-urlencoded OAuth 和 HTTP REST 请求

android - 访问 api.twitter.com 时出现 UnknownHostException