我使用HttpsUrlConnection
将json对象发送到服务器,如下所示
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();
}
和
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) {}
}
};
但它获得了像
这样的例外08-26 20:12:30.179:WARN / System.err(12477):java.io.IOException:主机名'services.myovs.com'未经过验证
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)< br / > 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):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-26 20:12:30.187:WARN / System.err(12477):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
08-26 20:12:30.187:WARN / System.err(12477):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-26 20:12:30.187:WARN / System.err(12477):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
08-26 20:12:30.187:WARN / System.err(12477):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935)
08-26 20:12:30.195:WARN / System.err(12477):在android.os.Handler.dispatchMessage(Handler.java:99)
08-26 20:12:30.195:WARN / System.err(12477):在android.os.Looper.loop(Looper.java:130)&gt; 08-26 20:12:30.195:WARN / System.err(12477):在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)
任何人都可以帮助我吗?
答案 0 :(得分:2)
据我所知,这不是例外。相反,它是写入日志的警告,包括堆栈跟踪。
警告表示连接不安全,因为无法验证主机名。它与您正在使用的HTTPS(而不是HTTP)连接有关。服务器可能没有由公知证书颁发机构颁发的证书。