我在我的应用程序中发出 POST 请求,有时(如果我有大量的 Post 数据),出现以下错误:
avax.net.ssl.SSLException: Write error: ssl=0x2f0610: I/O error during system call, Broken pipe
在下面的代码中执行 http.execute(httpost) 时。 有人知道如何避免这种情况吗?
我尝试使用 AndroidHttpClient,但找不到基本身份验证的有效方法 我尝试了 HttpsUrlConnection,但得到了同样的错误。
public static String makePOSTRequest(String s, List<NameValuePair> nvps,
String encoding) {
String ret = "";
UsernamePasswordCredentials c = new UsernamePasswordCredentials("XXX", "YYY");
BasicCredentialsProvider cP = new BasicCredentialsProvider();
cP.setCredentials(AuthScope.ANY, c);
HttpParams httpParams = new BasicHttpParams();
int connection_Timeout = 5000;
HttpConnectionParams.setConnectionTimeout(httpParams,
connection_Timeout);
HttpConnectionParams.setSoTimeout(httpParams, connection_Timeout);
DefaultHttpClient http = new DefaultHttpClient(httpParams);
http.setCredentialsProvider(cP);
HttpResponse res;
try {
HttpPost httpost = new HttpPost(s);
httpost.setEntity(new UrlEncodedFormEntity(nvps,
HTTP.DEFAULT_CONTENT_CHARSET));
res = http.execute(httpost);
InputStream is = res.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
res = null;
httpost = null;
ret = new String(baf.toByteArray(), encoding);
break;
} catch (ClientProtocolException e) {
ret = e.getMessage();
} catch (IOException e) {
ret = e.getMessage();
}
return ret;
}
编辑: 以下代码用于上传文件,如果我尝试上传小文件,代码可以工作,但如果文件变大,我会收到 broken pipe 错误。使用更快的 Internet 连接会增加文件大小,这似乎是服务器重置连接之前的时间问题。
public static boolean upload_image2(String url,
List<NameValuePair> nameValuePairs, File file, String encoding) {
boolean erg = false;
HttpParams httpParams = new BasicHttpParams();
int connection_Timeout = 120000;
HttpConnectionParams.setConnectionTimeout(httpParams,connection_Timeout);
HttpConnectionParams.setSoTimeout(httpParams, connection_Timeout);
http = new DefaultHttpClient(httpParams);
HttpResponse res;
UsernamePasswordCredentials c = new UsernamePasswordCredentials(username, password);
BasicCredentialsProvider cP = new BasicCredentialsProvider();
cP.setCredentials(AuthScope.ANY, c);
try {
HttpPost httpost = new HttpPost(url);
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.STRICT);
FileBody isb = new FileBody(file, "application/*");
entity.addPart("File", isb);
for (int index = 0; index < nameValuePairs.size(); index++) {
ContentBody cb;
// Normal string data
cb = new StringBody(nameValuePairs.get(index).getValue(),
"", null);
entity.addPart(nameValuePairs.get(index).getName(), cb);
}
httpost.setEntity(entity);
res = http.execute(httpost);
InputStream is = res.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
res = null;
httpost = null;
String ret = new String(baf.toByteArray(), encoding);
LastError = ret;
erg = true;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
LastError = e.getMessage();
erg = false;
} catch (IOException e) {
// TODO Auto-generated catch block
LastError = e.getMessage();
erg = false;
}
return erg;
}
最佳答案
我在使用 DefaultHttpClient 时遇到了同样的问题,嗯,使用我自己的 httpclient 实现来支持 https。小文件工作正常,大文件一次又一次失败。
阅读此回复后,我已经按照接受的答案建议更改为 HttpsURLConnection,而且因为它是由 android (http://android-developers.blogspot.pt/2011/09/androids-http-clients.html) 推荐的。
问题来了。原来问题出在服务器上,我之前更改了 PHP 服务器的配置以接受更大的尺寸,但我完全忘记更改 nginx 的 client_max_body_size
。进行小改动后,发送大文件就可以了。通过 HttpsUrlConnections 和 DefaultHttpClient。
关于java - SSL 破损管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8053824/