javaandroidsslssl-certificateokhttp

OkHTTP (v3.0.0-RC1) Post Request with Parameters


UPDATE I'm using okHttp library, version 3.0.0 RC-1. I have a url and I have to send a post request with parameters. For example: https://myurl.com/login?username=123456&password=12345

I have code like this:

 public String okHttpRequest(){

    try {
        final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

            }

            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        }
        };

        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null,trustAllCerts, new java.security.SecureRandom());
        final javax.net.ssl.SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.sslSocketFactory(sslSocketFactory);
        builder.hostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        }).build();
        final OkHttpClient client = new OkHttpClient();

        HttpUrl url = HttpUrl.parse("https://myUrl.com/login").newBuilder()
                .addQueryParameter("username", "123456")
                .addQueryParameter("password", "123456")
                .build();
        Request request = new Request.Builder()
                .url(url)
                .build();


        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                Log.d("Request:", "Is NOT sent " + request.toString() + " METHOD: " + request.method());
                e.printStackTrace();
            }

            @Override
            public void onResponse(Response response) throws IOException {
                Log.d("Request:", "Is sent " + response.toString());
            }
        });
    }catch(Exception e){
        e.printStackTrace();
    }
    return "okHttp is Working!!! ";

}    

Whenever I try, It fails, so onFailure method is executing. What is the problem? Am I adding request params incorrectly? Please help...


Solution

  • Yes, you are adding the query parameters incorrectly. Here's how it should be done:

    final OkHttpClient client = new OkHttpClient();
    
    HttpUrl url = HttpUrl.parse("https://myUrl.com/login").newBuilder()
            .addQueryParameter("password", "123456")
            .addQueryParameter("username", "123456")
            .build();
    
    Request request = new Request.Builder()
           .url(url)
           .build();
    (...)
    

    The problem is that you are submitting your data in the body of the request, as if it were a HTML form submit, and not as a query parameter, as you intended. Using HttpUrl allows you to add query parameters to your URL.

    Worth noting that you can also simply do this:

    Request request = new Request.Builder()
           .url("https://myurl.com/login?username=123456&password=12345")
           .build();
    

    So:

    Also, make sure you have internet permission in your app, make sure you have an active internet connection, etc, but it seems you already have.

    Note: this is for OkHttp 2.x (since that's what I use), but it should be the same or similar for 3.x.

    Let me know if it works for you.