在下面这段样例代码中我如何改造成调用https的接口··· //样例代码,获取手机号码归属地 public static void getMobileCodeInfo() throws ServiceException, RemoteException { Service service = new Service(); Call call = (Call) service.createCall(); // wsdl完整地址 call.setTargetEndpointAddress("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"); /** * 设置方法名 * new QName(String namespaceURI, String localPart) namespaceURI即为wsdl中的targetNamespace, localPart即为接口名 */ call.setOperationName(new QName("http://WebXml.com.cn/", "getMobileCodeInfo")); /** * 添加参数 * addParameter方法的参数包括:参数名(namespace+参数名)、参数类型、ParameterMode(入参即为IN) */ call.addParameter(new QName("http://WebXml.com.cn/", "mobileCode"), XMLType.XSD_STRING, ParameterMode.IN); call.setUseSOAPAction(true); // SOAPActionURI格式为targetNamespace+方法名 call.setSOAPActionURI("http://WebXml.com.cn/getMobileCodeInfo"); // 指定返回值类型,为字符串 call.setReturnType(XMLType.XSD_STRING); call.setReturnClass(java.lang.String.class); String result = (String) call.invoke(new Object[]{"13999903152"}); System.out.println(result); }···
时间: 2024-04-27 18:23:17 浏览: 157
您可以按照以下步骤将上述样例代码改造为调用https的接口:
1. 将wsdl地址中的http替换成https,即将以下代码:
call.setTargetEndpointAddress("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
改为:
call.setTargetEndpointAddress("https://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl");
2. 创建SSLContext实例,并设置信任管理器TrustManager来信任所有证书:
```java
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
//...
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
}
}
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
```
3. 设置HttpsURLConnection默认的SSLSocketFactory和HostnameVerifier:
```java
import javax.net.ssl.HttpsURLConnection;
//...
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier((hostname, sslSession) -> true);
```
4. 将调用方式改为使用HttpURLConnection,即将以下代码:
String result = (String) call.invoke(new Object[]{"13999903152"});
改为:
URL url = new URL("https://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
connection.setDoOutput(true);
String soapRequest = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
" <soap:Body>\n" +
" <getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">\n" +
" <mobileCode>13999903152</mobileCode>\n" +
" <userID></userID>\n" +
" </getMobileCodeInfo>\n" +
" </soap:Body>\n" +
"</soap:Envelope>";
OutputStream outputStream = connection.getOutputStream();
outputStream.write(soapRequest.getBytes("UTF-8"));
String result = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
result = stringBuilder.toString();
}
System.out.println(result);
```
请注意替换代码中的mobileCode参数值和soapRequest中的namespace和方法名。同时,为了保证代码的安全性,请在实际使用过程中,不要信任所有证书,可以使用合法的证书或者自己实现TrustManager来进行证书校验。
阅读全文