gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 02:22:59 26 4
gpt4 key购买 nike

目前我使用的是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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com