我让我的应用程序和somtimes POST请求(如果我有一个巨大的后的数据量),出现下列错误:SSL破裂的管道
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;
}
编辑: 下面的代码是用于上传文件,如果我尝试上传小文件,代码工作,但如果文件越来越大,我收到破裂的管道错误。使用更快的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;
}
请参阅http://stackoverflow.com/questions/2899079/custom-ssl-handling-stopped-working-on-android-2-2-froyo/2906293#comment7925084_2906293 – tonys
thx,但已经尝试,同样的错误。我使用代理的请求,但在我的WM应用程序它运行没有错误,必须依赖于Android系统 – 2red13