SSL破裂管道

13

我在应用程序中进行POST请求,有时(如果POST数据量很大),会出现以下错误:

avax.net.ssl.SSLException:写入错误:ssl=0x2f0610:系统调用期间的I/O错误,管道已断开

在下面的代码中执行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;
}

编辑: 以下代码用于上传文件,如果我尝试上传小文件,则代码正常工作,但如果文件变大,则会收到断开管道错误。使用更快的互联网连接将增加文件大小,似乎是因为服务器重置连接的时间问题。

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;
}

请参见https://dev59.com/93E85IYBdhLWcg3wMAfc#9xgBoYgBc1ULPQZFyX_4。 - tonys
谢谢,但我已经尝试过了,还是出现同样的错误。我在请求中使用了代理,但在我的 WM 应用程序中它可以正常运行,可能是与 Android 系统有关。 - 2red13
有时候这是由于网络连接问题引起的。我也遇到了这个问题。 - Ahmed Jehanzaib
3个回答

6

我使用DefaultHttpClient遇到了同样的问题,我的httpclient实现支持https。小文件的情况下运行良好,而大文件则一次又一次地失败。

阅读了这个响应之后,我按照被接受的答案建议使用HttpsURLConnection,也因为Android推荐(http://android-developers.blogspot.pt/2011/09/androids-http-clients.html)。

问题依然存在。事实证明,问题出在服务器上,我已经在之前更改了PHP服务器的配置以接受更大的大小,但是我完全忘记更改nginx的client_max_body_size。做出这个小改变后,通过HttpsUrlConnections和DefaultHttpClient发送大文件都能正常工作。


3
我曾经也遇到过同样的错误。问题出在Android库中使用的DefaultHttpClient,它自Android API版本1至今一直存在,而AndroidHttpClient则自Android API版本8开始提供。这是Android中的一个错误https://code.google.com/p/android/issues/detail?id=8625
我的问题是: 默认超时时间为60秒。我在Wireshark中运行连接时,握手已经建立,但它一直等待ApplicationData,但却没有得到它,所以在超时后发送FIN,我就收到了:javax.net.ssl.SSLException: Write error: ssl=0x2f0610: I/O error during system call, Broken pipe.
我通过设置http连接的超时时间为5分钟或更长时间来解决了我的问题。如果可以推荐如何解决你的问题,请在服务器上运行Wireshark并监听与移动设备的通信。

1

我无法通过DefaultHttpClient、AndroidHttpClient或Abstract来解决问题,但最终通过HttpsUrlRequest和通过头部进行身份验证找到了解决方案,而不是使用CredentielsService:

public static boolean upload_image5(String urls,File file, String encoding){
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;
    String myfilename = file.getName();
    String urlServer = urls;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";
    boolean erg = false;
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;

    try
    {
    FileInputStream fileInputStream = new FileInputStream(file);

    URL url = new URL(urlServer);
    connection = (HttpsURLConnection) url.openConnection();

    // Allow Inputs & Outputs
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);

    // Enable POST method
    connection.setRequestMethod("POST");

    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
    connection.addRequestProperty("Authorization","Basic [YOUR MD5 LOGIN VALUE]");
    outputStream = new DataOutputStream( connection.getOutputStream() );
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);

    outputStream.writeBytes("Content-Disposition: form-data; name=\"DestFileName\"");
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(myfilename);
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes("Content-Disposition: form-data; name=\"Target\"" );
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes("DOC");
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes("Content-Disposition: form-data; name=\"filename\"");
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(myfilename);
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes("Content-Disposition: form-data; name=\"File\"; filename=\"" + myfilename + "\"");
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes("Content-Type: application/*");
    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(lineEnd);
    //hier File schreiben
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    buffer = new byte[bufferSize];
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    while (bytesRead > 0)
    {
    outputStream.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    }

    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);


    fileInputStream.close();


    try {
        inputStream = new DataInputStream(connection.getInputStream()); 
        StringBuilder response = new StringBuilder();
        String line;
        while ((line = inputStream.readLine()) != null) {
            response.append(line).append('\n');
        }
        LastError =response.toString();
        erg = true;
    } catch (IOException e) { 
        LastError = e.getMessage();
        erg = false;
    } finally { 
        if (inputStream != null){
            try { 
                inputStream.close(); 
            } catch (IOException e) { 
                e.printStackTrace();
            } 
        }
    }

    outputStream.flush();
    outputStream.close();
    }
    catch (Exception ex)
    {
        LastError = ex.getMessage();
        erg = false;
    }
    return erg;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接