SSL断管

扫码查看
本文介绍了SSL断管的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序和somtimes中发出POST请求(如果我有大量的Post数据),发生以下错误:

I make POST requests in my application and somtimes (if i have a huge amount of Post data), the following error occured:

while在下面的代码中执行http.execute(httpost)。
有谁知道如何避免这种情况?

while executing http.execute(httpost) in the code below.Does anybody know how to avoid this?

我试图使用AndroidHttpClient,但找不到基本auth的有效方式
和我尝试了一个HttpsUrlConnection,但得到了同样的错误。

I tryed to use AndroidHttpClient, but can't find a valid way for basic authAnd i tryed a HttpsUrlConnection, but get the same error.

    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连接会增加文件大小,这似乎是服务器重置连接之前的时间问题。

edit:The following code is used for uploading files, if I try to upload small files, the code works, but if the files get bigger, i receive the broken pipe error. Using a faster Internetconnection will increase the filesize, it seemed to be a problem withe the time until the server is resetting the connection.

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,AndroidHttpClient或Abstract的问题,但最终找到了一个HttpsUrlRequest的解决方案,通过头而不是CredentielsService进行身份验证:

I wasn't able to fix the problem with DefaultHttpClient, AndroidHttpClient or Abstract, but finally found a solution with HttpsUrlRequest ant Authentication via header instead of 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;
}

这篇关于SSL断管的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 14:03
查看更多