Service.asmx接口和http接口的区别
时间: 2024-04-27 14:25:04 浏览: 11
Service.asmx接口是一种基于SOAP协议的Web服务,而HTTP接口则是一种基于HTTP协议的Web服务。它们的区别主要体现在以下几个方面:
1. 数据传输格式:Service.asmx接口使用SOAP格式的XML文档进行数据传输,而HTTP接口则可以使用多种格式进行数据传输,包括JSON、XML、二进制等。
2. 调用方式:Service.asmx接口通常使用POST请求进行调用,而HTTP接口可以使用GET、POST、PUT、DELETE等不同的HTTP方法进行调用。
3. 传输效率:由于SOAP格式的XML文档比较冗长,因此Service.asmx接口传输效率相对较低,而HTTP接口则可以使用较为紧凑的数据格式,传输效率相对较高。
4. 跨平台支持:由于SOAP格式的XML文档需要使用SOAP协议进行解析,因此Service.asmx接口对于不同的平台和语言的支持存在一定的限制,而HTTP接口则可以在各种不同的平台和语言中进行调用。
总之,Service.asmx接口和HTTP接口都是Web服务的一种形式,它们的选择应该根据具体的应用场景和需求进行考虑。如果需要支持跨平台调用,或者需要较高的传输效率,建议使用HTTP接口;如果需要使用SOAP格式的XML文档进行数据传输,或者需要使用特定的Web服务协议,建议使用Service.asmx接口。
相关问题
在下面这段样例代码中我如何改造成调用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); }···
您可以按照以下步骤将上述样例代码改造为调用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来进行证书校验。
服务方使用webservice中HTTP post的请求地址为http://ip:port/DoReqToHis.asmx/getDeptInfo,Content-Type为application/x-www-form-urlencoded。调用方使用的http接口希望post的请求地址为http://ip:port/doReqToHis?service=getHospitalInfo,且Content-Type为text/xml; charset=utf-8,服务方该如何配合调用方进行webservice的调整
服务方需要进行以下调整:
1. 修改webservice的地址为http://ip:port/doReqToHis;
2. 修改webservice方法名为getHospitalInfo;
3. 修改请求报文的Content-Type为text/xml; charset=utf-8。
同时,服务方需要通知调用方进行相应的修改。