Reputation: 2267
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 ?
Upvotes: 0
Views: 4235
Reputation: 78825
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.
Upvotes: 2