使用HostnameVerifier调用WSDL服务方法?

时间:2014-07-24 11:14:28

标签: android web-services ssl soap wsdl

我有一个wsdl服务和一个通过HttpsUrlConnection连接到服务的android客户端。 wsdl服务包含两种调用测试方法的方法:

HostnameVerifier hostnameVerifier = new HostnameVerifier() {

    @Override
    public boolean verify(String hostname, SSLSession session)
    {
        if (hostname.equals(SSL_HOSTVERIFIER_TAG))
            return true;
        else
            return false;
    }
};

CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = mContext.getResources().openRawResource(R.raw.firm_gmbh);

Certificate ca;
try
{
    ca = cf.generateCertificate(caInput);
} finally{
    caInput.close();
}

String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);

String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);

SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);

String authString = mUserName + ":" + mPassword;
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);

URL url = new URL(SSL_URL);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
urlConnection.setSSLSocketFactory(context.getSocketFactory());
urlConnection.setHostnameVerifier(hostnameVerifier);
urlConnection.setRequestMethod("GET");
urlConnection.setRequestProperty("soapaction", SOAP_ACTION);

int res = urlConnection.getResponseCode();
String message = urlConnection.getResponseMessage();
InputStream in = urlConnection.getInputStream();
String msg = convertStreamToString(in);

我得到一个连接,现在我想调用一个服务方法,但我不知道该怎么做。我尝试使用kso​​ap2 lib,因此我可以使用调用服务soap操作的HttpTransport.call方法。但结合ssl连接,它没有用。

我现在的问题是:如何使用我的代码调用特定的Soap操作或wsdl元素,而不会丢失TrustAllCertsNullHostnameVerifier等任何安全功能。

PS:我已尝试addRequestProperties("SOAPAction",SOAP_ACTION"),但它以FileNotFoundException回复。

0 个答案:

没有答案