質問

KSOAP2-androidを使用して、SSLを介したWCFサービスに電話をかけています。 SSLなしで動作させることができますが、今ではSSLを介して電話をかけたいと思っていますが、いくつかの問題に遭遇しています。

httptransportseの代わりにhttpstransportseを使用していますが、エラーが発生しています:javax.net.ssl.sslexception:信頼されていないサーバー証明書

どうすればこれを修正できますか?

Androidのキーストアにサーバー証明書を追加して問題を解決できますか?

private static final String SOAP_ACTION = "http://example.com/Service/GetInformation";
private static final String METHOD_NAME = "GetInformation";
private static final String NAMESPACE = "http://example.com";    
private static final String URL = "dev.example.com/Service.svc";

public static Result GetInformation()
{
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

    PropertyInfo property = new PropertyInfo();
    property.name = "request";

    Request request =
        new Request("12", "13", "Ben");

    userInformationProperty.setValue(request);
    userInformationProperty.setType(request.getClass());
    request.addProperty(property);

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    envelope.addMapping(NAMESPACE, "Request",new Request().getClass());

    HttpsTransportSE transport = new HttpsTransportSE(URL, 443, "", 1000);

    //HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    transport.debug = true;

    try
    {
        transport.call(SOAP_ACTION, envelope);          
        return Result.FromSoapResponse((SoapObject)envelope.getResponse());
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    catch (XmlPullParserException e)
    {
        e.printStackTrace();
    }

    return null;
}
役に立ちましたか?

解決

まあ、httpsserviceconnectionseを変更する代わりに、これを行う簡単な方法があります。説明されているように、偽の信頼マネージャーをインストールできます http://groups.google.com/group/android-developers/browse_thread/thread/1ac2b851e07269ba/c7275ff3b28ad8bc?lnk=gst&q=certificate そして、ksoap2へのSSL通信/呼び出しを行う前に、lowallssl()を呼び出します。新しいデフォルトのhostnameverifierとTrustmanagerを登録します。 KSOAP2は、SSL通信を行うときにデフォルトの通信を使用し、魅力のように機能します。

また、これにさらに努力し、(より多くの)より安全にし、アプリケーションのローカルトラストマネージャーに証明書をインストールすることもできます。私は安全なネットワークにいて、中間の攻撃を恐れていなかったので、最初のことをしました。

このようにkeepalivehttpstransportseを使用する必要があることがわかりました new KeepAliveHttpsTransportSE(host, port, file, timeout);. 。パラメーターはURLオブジェクトに移動するため、jiraのインストールにアクセスするために new KeepAliveHttpsTransportSE("host.whatever", 443, "/rpc/soap/jirasoapservice-v2", 1000).

エミュレータではなくJ2SE環境でも、J2SE/ME KSOAP2ライブラリで(KeepAlive)ではなく、J2SE環境でプレイするために使用するテクノロジーやWebサービスに慣れていない場合に便利な場合があります。 httpstransportseのものはありません(KSOAP2-J2SE-FULL-2.1.2.JARを使用しました)。あなたができることは、Androidスピンオフksoap2-androidから3つのクラスhttpstransportse、keepalivehttpstransportse、およびhttpsserviceconnectionseのソースを取得することです。それは私のために働き、未知で非常に複雑なWebサービスで最初のステップを正しくするために生産性の向上になりました。

他のヒント

いくつかのソースコードでヴェドランの答えを補完するために、申し訳ありませんが、コメントできません。

Trustmanager:

private static TrustManager[] trustManagers;

public static class _FakeX509TrustManager implements
        javax.net.ssl.X509TrustManager {
    private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};

    public void checkClientTrusted(X509Certificate[] arg0, String arg1)
            throws CertificateException {
    }

    public void checkServerTrusted(X509Certificate[] arg0, String arg1)
            throws CertificateException {
    }

    public boolean isClientTrusted(X509Certificate[] chain) {
        return (true);
    }

    public boolean isServerTrusted(X509Certificate[] chain) {
        return (true);
    }

    public X509Certificate[] getAcceptedIssuers() {
        return (_AcceptedIssuers);
    }
}

public static void allowAllSSL() {

    javax.net.ssl.HttpsURLConnection
            .setDefaultHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });

    javax.net.ssl.SSLContext context = null;

    if (trustManagers == null) {
        trustManagers = new javax.net.ssl.TrustManager[] { new _FakeX509TrustManager() };
    }

    try {
        context = javax.net.ssl.SSLContext.getInstance("TLS");
        context.init(null, trustManagers, new SecureRandom());
    } catch (NoSuchAlgorithmException e) {
        Log.e("allowAllSSL", e.toString());
    } catch (KeyManagementException e) {
        Log.e("allowAllSSL", e.toString());
    }
    javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(context
            .getSocketFactory());
}

あなたの方法での呼び出し:

allowAllSSL();
HttpsTransportSE httpsTransport = new HttpsTransportSE(Server,443, URL, 1000);

ノート:

  1. サーバーはサーバーURLです。
  2. 443はデフォルトのHTTPSポートですが、コンストラクターが予想されるため、ポートを指定する必要があります。
  3. WS操作へのパスをURLします
  4. タイムアウトの1000 E

https:// server:443/url]として構築されています

私のために働くksoap + webサービスwcf with eclipse

private static SoapObject getBody(final SoapSerializationEnvelope soapEnvelope) throws Exception {
        if (soapEnvelope.bodyIn == null) {
            throw new Exception("soapEnvelope.bodyIn=null");
        }
        else if (soapEnvelope.bodyIn.getClass() == SoapFault.class) {
            throw new ExceptionLogic((SoapFault) soapEnvelope.bodyIn));
        }
        else {
            return (SoapObject) soapEnvelope.bodyIn;
        }

    }

private static SoapSerializationEnvelope sendRequete(final SoapObject soapReq, final String classMappingName,
            final Class<?> classMapping, final int timeOutSpecial) {



        final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.implicitTypes = true;
        soapEnvelope.dotNet = true;

        if (classMappingName != null) {
            soapEnvelope.addMapping(NAMESPACE, classMappingName, classMapping);
        }

        soapEnvelope.setOutputSoapObject(soapReq);

        try {

            final HttpTransportSE httpTransport = new HttpTransportSE(Constante.urlWebService, timeOutSpecial);
            httpTransport.debug = BuildConfig.DEBUG;

            // Prod
            if (Constante.urlWebService.startsWith("https://")) {
                final List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
                headerList.add(new HeaderProperty("Authorization", "Basic "
                        + org.kobjects.base64.Base64.encode((Constante.CERTIFICAT_LOGIN + ":" + Constante.CERTIFICAT_MDP).getBytes())));

                FakeX509TrustManager.allowAllSSL();
                httpTransport.call(NAMESPACE + "/" + soapReq.getName(), soapEnvelope, headerList);
            }
            // Test
            else {
                httpTransport.call(NAMESPACE + "/" + soapReq.getName(), soapEnvelope);
            }

            return soapEnvelope;
        }
        catch (final Exception e) {
            throw new Exception("Erreur : " + e.getMessage(), e);
        }

    }



    private static class FakeX509TrustManager implements X509TrustManager {
        private static TrustManager[] trustManagers;
        private final X509Certificate[] _AcceptedIssuers = new X509Certificate[] {};

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

        public static void allowAllSSL() {
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

                @Override
                public boolean verify(final String hostname, final SSLSession session) {
                    return true;
                }
            });
            SSLContext context = null;
            if (trustManagers == null) {
                trustManagers = new TrustManager[] { new FakeX509TrustManager() };
            }
            try {
                context = SSLContext.getInstance("TLS");
                context.init(null, trustManagers, new SecureRandom());
            }
            catch (final NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            catch (final KeyManagementException e) {
                e.printStackTrace();
            }
            HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
        }

        @Override
        public void checkClientTrusted(final X509Certificate[] arg0, final String arg1) throws CertificateException {

        }

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

        }
    }

はい、おそらくこれを試してみることができます

HTTPS接続Android

これに関して問題トラッカーに提出されたバグがありました

http://code.google.com/p/android/issues/detail?id=2388

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top