【发布时间】:2014-09-30 01:20:03
【问题描述】:
目前我正在使用 Apache http 组件客户端 V4.3.5。在我的情况下,我可以上传小文件(1kb),但是当我运行代码并得到异常“org.apache.http.NoHttpResponseException:192.168.128.109:443 failed to respond时,它不适用于大文件(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();
}
}
谢谢, 比尔
【问题讨论】:
-
我们使用 python 将相同的文件上传到服务器并且它正在工作。这个问题只发生在 Java 中。我的 JDK 是 java 版本“1.7.0_67”。
-
我通过覆盖 HTTP 客户端中的 HttpRequestRetryHandler 来修复它。 stackoverflow.com/questions/10570672/…
标签: httpclient