Android HTTPS Client不从套接字读取数据

时间:2012-09-11 17:53:02

标签: java android ssl https httpclient

我正在开发一个应用程序,它将从C ++服务器(目前在我的本地网络上)读取SSL加密的字符串数据,并以实物形式返回信息。

我已经关注了许多有关bouncycastle提供程序,自定义套接字工厂,http客户端的教程和许多示例,我已经达到了能够从模拟器向我的c ++服务器发送信息的程度,但模拟器无法读取来自套接字的数据。相反,我得到:

  

java.net.SocketTimeoutException:读取超时

服务器识别握手和连接,并读取模拟器写入的传入语句。

在AsyncTask线程中调用请求

private class getXmlTask extends AsyncTask<String, String, String> { 

    protected void onPreExecute() {
        pd = ProgressDialog.show(NewJobsView.this,"Retrieving Data","Please Wait",true,false);
        pd.setCancelable(true);

    }
    protected void onPostExecute(String XML) {
        Log.i("XML:", XML);
        parseXml();
        pd.dismiss();
    }

    @Override
    protected String doInBackground(String... params) {
        return getXml();
    }   
}

这是我正在使用的请求类(请原谅,如果它有点乱):

public class HttpRequest{

    MyHttpClient httpClient;
    HttpContext localContext;
    private String ret;

    HttpResponse response = null;
    HttpPost httpPost = null;
    HttpGet httpGet = null;
    Context context = null;   

    int retry = 0;



    public HttpRequest(Context c){
        HttpParams params = new BasicHttpParams();

        context = c;

        HttpConnectionParams.setConnectionTimeout(params, 60000);
        HttpConnectionParams.setSoTimeout(params, 60000);
        HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
        HttpProtocolParams.setUseExpectContinue(params, true);

        httpClient = new MyHttpClient(context);             

        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);       


        httpClient.setParams(params);
        localContext = new BasicHttpContext();    
    }


    public void abort() {
        try {
            if (httpClient != null) {
                System.out.println("Abort.");
                httpPost.abort();
            }
        } catch (Exception e) {
            System.out.println("MyApp" + e);
        }
    }

    public String sendPost(String url, String data) {
        return sendPost(url, data, null);
    }    

    public String sendPost(String url, String data, String contentType) {
        ret = null; 

        httpPost = new HttpPost(url);
        httpPost.addHeader("User-Agent", data);
        response = null;               

        StringEntity ent = null;        

        try {
            ent = new StringEntity(data,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            Log.e("MyApp", "HttpUtils : UnsupportedEncodingException : "+e);
        }

        httpPost.setEntity(ent);

        try {

            response = httpClient.execute(httpPost);

            if (response != null) {
                ret = EntityUtils.toString(response.getEntity());
                Log.e("MyApp", "HttpUtils: " + ret);
            }
            else{
                abort();
            }

        } catch (Exception e) {
            Log.e("MyApp", "HttpUtils: " + e);
        }

        Log.d("MyApp", "Returning value:" + ret);

        if(retry < 3){
            sendPost(url, data);
            retry = retry +1;
        }
        else{
            return ret;
        }

        return ret;

    }  

}

其他几点说明:

我有

  

uses-permission android:name =“android.permission.INTERNET”

在我的清单文件中。

我还验证了SSL服务器的工作原理,因为其他客户端(尽管是C ++)能够维护连接并从套接字读取。

MyHttpClient对象是

的近似副本

How to create a BKS (BouncyCastle) format Java Keystore that contains a client certificate chain

我一直在旋转我的车轮,任何答案都会非常感激。

谢谢!

1 个答案:

答案 0 :(得分:0)

首先检查您的网址,然后尝试使用一个建议httpsconnection

它比普通的httpGet好,因为有时它会给你sockettime / ssl或任何其他io异常