0

I am sending a json object to the server using HttpsUrlConnection as following

try{ 
    JSONObject jobj=new JSONObject();

    jobj.put("UserName","demo1");
    jobj.put("Password","demo1");
    Log.e("json string :",jobj.toString());

    byte[] postData = jobj.toString().getBytes();

    URL url = new URL("https://services.myovs.com/TokenService/Token");

    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setUseCaches(false);

    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/json");

    OutputStream out = conn.getOutputStream();

    out.write(postData);
    out.close();

    String temp = ((HttpsURLConnection)conn).getResponseMessage();
    Log.e("response  :",temp);
} catch(Exception ex) {
    ex.printStackTrace();   
}

and

TrustManager[] trustAllCerts = new TrustManager[] {
    new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
        public void checkClientTrusted(
                java.security.cert.X509Certificate[] certs,
                String authType) {}
        public void checkServerTrusted(
                java.security.cert.X509Certificate[] certs,
                String authType) {}
    }
};

but its getting an exception like

08-26 20:12:30.179: WARN/System.err(12477): java.io.IOException: Hostname 'services.myovs.com' was not verified
08-26 20:12:30.187: WARN/System.err(12477): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.verifySecureSocketHostname(HttpConnection.java:199)
08-26 20:12:30.187: WARN/System.err(12477): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeConnection(HttpsURLConnectionImpl.java:391)
08-26 20:12:30.187: WARN/System.err(12477): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)
08-26 20:12:30.187: WARN/System.err(12477): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614)
08-26 20:12:30.187: WARN/System.err(12477): at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:268)
08-26 20:12:30.187: WARN/System.err(12477): at com.example.httpconnection.Main.httpMethode(Main.java:69)
08-26 20:12:30.187: WARN/System.err(12477): at com.example.httpconnection.Main.onCreate(Main.java:27)
08-26 20:12:30.187: WARN/System.err(12477): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-26 20:12:30.187: WARN/System.err(12477): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-26 20:12:30.187: WARN/System.err(12477): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-26 20:12:30.187: WARN/System.err(12477): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-26 20:12:30.187: WARN/System.err(12477): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-26 20:12:30.195: WARN/System.err(12477): at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 20:12:30.195: WARN/System.err(12477): at android.os.Looper.loop(Looper.java:130) > 08-26 20:12:30.195: WARN/System.err(12477): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-26 20:12:30.195: WARN/System.err(12477): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 20:12:30.195: WARN/System.err(12477): at java.lang.reflect.Method.invoke(Method.java:507)
08-26 20:12:30.195: WARN/System.err(12477): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-26 20:12:30.195: WARN/System.err(12477): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-26 20:12:30.195: WARN/System.err(12477): at dalvik.system.NativeStart.main(Native Method)

Can anyone help me ?

2
  • Can you please post the whole stacktrace? Commented Aug 26, 2012 at 14:35
  • i edited the response please find that Commented Aug 26, 2012 at 14:48

1 Answer 1

2

As far as I can tell, it's not an exception. Instead it's a warning written to the log including a stacktrace.

The warning indicates that the connection is not secure because the hostname could not be verified. It has to do with the HTTPS (as opposed to a HTTP) connection you're using. The server probably doesn't have a certificate issued by a publicly known certificate authority.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.