Android和HTTP中的SSL套接字

时间:2014-02-12 13:18:37

标签: android sslsocketfactory

我正在尝试使用NamePairValues将图像字符串从android发送到服务器。它可以正常使用http,但不适用于Https。使用Https我的代码在移动应用上显示错误响应:

javax.net.ssl.SSLException: Write error: ssl=0x12ad700: I/O error during system call, Connection reset by peer

请帮我解决这个问题。我使用了与SSL层完全相同的代码。我使用过SSL Socket Factory泛型类。这是代码片段。

以下是来自App。

的致电热线
 nameValuePairs.add(new BasicNameValuePair("image", sendImages));
                   // nameValuePairs.add(new BasicNameValuePair("type", "L0"));
                    pBar = new ProgressDialog(this);
                    pBar.setCanceledOnTouchOutside(false);
                    pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    pBar.setMessage("Communication With Server, Authenticating...");
                    pBar.show();

                    MyAsyncTask1 mTask = new MyAsyncTask1();

                    mTask.setOnResultsListener(this);

                    mTask.execute(myURL
                            , nameValuePairs);

这是一个执行任务的类

public class MyAsyncTask1 extends AsyncTask<Object, Integer, String> {

    ResultsListener listener;


    public void setOnResultsListener(ResultsListener listener) {
        this.listener = listener;
    }

    @Override
    protected String doInBackground(Object... params) {
        // TODO Auto-generated method stub
        String res = null;
        DefaultHttpClient responseBody = null;
        if (params.length > 0) {
            String mUrl =  params[0].toString();
            List<NameValuePair> mParams = (List<NameValuePair>) params[1];

            //String pam = "param1="+"dsd"+"&param2="+mobile+"&param3=" + "sds"+"&param4="+"sdsds"+"&param5="+"dsds"+"&param6="+Images;
            //String regx = "[] ";
            //char[] ca = regx.toCharArray();
            //for (char c : ca) {
              //  pam = pam.replace(""+c, "");
               // pam = pam.replace(",", "&");

          //  }

            try{

                KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
                trustStore.load(null, null);

                MySSLSocketFactory socketFactory = new MySSLSocketFactory(trustStore);

                 SchemeRegistry registry = new SchemeRegistry();
                    registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
                    registry.register(new Scheme("https", socketFactory, 443));


                socketFactory.setHostnameVerifier(socketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);


                DefaultHttpClient httpclient= new DefaultHttpClient();              
                httpclient.getConnectionManager().getSchemeRegistry().register(registry);
                HttpPost httpget = new HttpPost(mUrl);
                 httpget.setEntity(new UrlEncodedFormEntity(mParams));
            //  System.out.println("executing request " + httpget.getRequestLine());

                HttpResponse response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity();

                //System.out.println("----------------------------------------");
                //System.out.println(response.getStatusLine());
                //if (entity != null) {
                //   System.out.println("Response content length:  " + entity.getContentLength());
                //}

                // Print html.
                BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                String line = "";
                while ((line = in.readLine()) != null) {
                    res=line; 
                    System.out.println(line);
                }
                in.close();

                }catch(Exception e){

                    return res=e.toString();

                }

            }
        return res;
    }
    @Override
    protected void onPostExecute(String result) {
    //Toast.makeText(getBaseContext(), result, Toast.LENGTH_SHORT).show();
        if (!listener.equals(null)) {
            listener.onResultsSucceeded(result);
        }
    }

    public HttpClient getNewHttpClient() {
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);

            MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);

            sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));

            ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

            return new DefaultHttpClient(ccm, params);
        } catch (Exception e) {
            return new DefaultHttpClient();
        }
    }

}

1 个答案:

答案 0 :(得分:-1)

你为你的httpclient做了一个getNewHttpClient()方法而没有使用?

DefaultHttpClient httpclient= new DefaultHttpClient();              
...

尝试这样的事情:

try {
    DefaultHttpClient httpClient = new DefaultHttpClient();

    httpClient = (DefaultHttpClient) getNewHttpClient();

    HttpPost httpget = new HttpPost(mUrl);
    httpget.setEntity(new UrlEncodedFormEntity(mParams));

    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    BufferedReader in = new BufferedReader(new InputStreamReader(
            response.getEntity().getContent()));
    String line = "";
    while ((line = in.readLine()) != null) {
        res = line;
        System.out.println(line);
    }
    in.close();
} catch (Exception e) {
    return res = e.toString();
}