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): org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.verifySecureSocketHostname(HttpConnection.java:199)
08-26 20:12:30.187: WARN/System.err(12477): 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): org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:614) で
08-26 20:12:30.187: WARN/System.err(12477): org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:268) で
08-26 20:12 :30.187: WARN/System.err(12477): com.example.httpconnection.Main.httpMethode(Main.java:69)
08-26 20:12:30.187: WARN/System.err(12477): 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) > 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): java.lang.reflect.Method.invokeNative(ネイティブ メソッド)
08で-26 20:12:30.195: WARN/System.err(12477): java.lang.reflect.Method.invoke(Method.java:507)
08-26 20:12:30.195: WARN/System.err(12477) ): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-26 20:12:30.195: WARN/System.err(12477): com.android.internal.os で。 ZygoteInit.main (ZygoteInit.java:625)
08-26 20:12:30.195: 警告/System.err (12477): dalvik.system.NativeStart.main (ネイティブ メソッド) で
誰でも私を助けることができますか?