0

since i am new in android studio, i wanted to consume my own web service (REST Client) im trying to execute httprequest in android. the url is using https ssl certificate , and i get error

javax.net.ssl.SSLPeerUnverifiedException: No peer certificate android studio

this is my code :

 private void executeRequest(HttpUriRequest request, String url)
 {
  HostnameVerifier hostnameVerifier =   org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;


    DefaultHttpClient client = new DefaultHttpClient();

    SchemeRegistry registry = new SchemeRegistry();
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier)  hostnameVerifier);
    registry.register(new Scheme("https", socketFactory, 443));
    SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
    DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());

   HttpClient client = new DefaultHttpClient();
    HttpResponse httpResponse;

    try {
        StrictMode.ThreadPolicy policy = new  StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        httpResponse = client.execute(request);
        responseCode = httpResponse.getStatusLine().getStatusCode();
        message = httpResponse.getStatusLine().getReasonPhrase();

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {

            InputStream instream = entity.getContent();
            response = convertStreamToString(instream);

            // Closing the input stream will trigger connection release
            instream.close();
        }

    } catch (ClientProtocolException e)  {
        client.getConnectionManager().shutdown();
        e.printStackTrace();
    } catch (IOException e) {
        client.getConnectionManager().shutdown();
        e.printStackTrace();

    }
}

i also tried this code to, but not workin too

public static HttpClient createHttpClient()
{
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);

    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);

    return new DefaultHttpClient(conMgr, params);
}

thanks

gilllsss
  • 97
  • 2
  • 11
  • 1
    Have you checked that the server you are hitting has a proper certificate chain? see http://stackoverflow.com/questions/18126372/safely-fixing-javax-net-ssl-sslpeerunverifiedexception-no-peer-certificate – hack_on Feb 28 '17 at 05:15
  • hey hack_on, how to check that my url has a proper certificate chain , it has https and ssl certificate i guess. – gilllsss Feb 28 '17 at 05:24
  • hey, i already tested in, and i got A and trusted = Yes. is anything i can do more ? pls im so depressed about this problem – gilllsss Feb 28 '17 at 05:32
  • Hit it from the default browser on an Android device -- does that work? How does Android/Java know to trust the server -- is the top certificate that signed the server's certificate trusted by the browser already? See http://stackoverflow.com/questions/4461360/how-to-install-trusted-ca-certificate-on-android-device for how to do this. – hack_on Feb 28 '17 at 05:32
  • yes its work, i think there is no problem with the url , its because my code. maybe i missed something ? – gilllsss Feb 28 '17 at 05:35
  • If you change the URL to "https://www.google.com.au/search?q=google+https+test&ie=utf-8&oe=utf-8&gws_rd=cr" in the code, does it work / get further? Or same error? (I assume you can get to internet). – hack_on Feb 28 '17 at 05:38

1 Answers1

0

Have you checked the Configuration file , to allow network connection ? check a xml config file and try to allow internet connection.

Joel Wembo
  • 814
  • 6
  • 10