简体   繁体   中英

Android HTTPS Clientnot reading data from socket

I'm developing an application that will read SSL encrypted string data from a C++ server (presently on my local network), and return information in kind.

I have followed many tutorials and many examples regarding bouncycastle providers, custom socket factories, http clients, and I have got to the point where I am able to send information from the emulator to my c++ server, but the emulator cannot read the data from the socket. Instead I get:

java.net.SocketTimeoutException: Read timed out

The server recognizes the handshake and connection, and reads the incoming statement the emulator writes.

The request is being called within an AsyncTask thread

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

This is the request class I'm using (please excuse if it's a bit messy):

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;

    }  

}

A few other notes:

I have

uses-permission android:name="android.permission.INTERNET"

in my Manifest file.

I also have verified that the SSL server works, as other clients (albeit C++) are able to maintain a connection and read from the sockets.

the MyHttpClient object is a near replica of

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

I've been spinning my wheels for a long time, and any answer would be greatly appreciated.

Thank you!

check your URL first and one suggestion try to use httpsconnection

it is better than normal httpGet as some times it will gives you sockettime / ssl or any other io exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM