C#接口的类型是asmx接口
步骤
导入pom
<!--web service相关-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
编写传输类
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;
import java.rmi.RemoteException;
public class CallWebservice {
/**
* 调用webservice.asmx接口 获取数据
*/
public static String callWebserviceASMX() throws ServiceException, RemoteException {
String url = "http://.......................asmx";// 提供接口的地址
String soapaction = "http://.........WebServiceExtend/"; // 域名
String method = "method1"; //方法名
String usertoken = "value1"; //方法参数1...
String countryType = "value2"; //方法参数2....(更多的话就继续写)
String info = ""; //返回的数据
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
call.setOperationName(new QName(soapaction, method)); // 要调用哪个方法(method中定义)
call.addParameter(new QName(soapaction, "usertoken"), // 要传递的参数1--要和接口方提供的参数名一致,
org.apache.axis.encoding.XMLType.XSD_STRING, //指明参数string类型
javax.xml.rpc.ParameterMode.IN);
call.addParameter(new QName(soapaction, "countryType"), // 要传递的参数2
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);
call.setReturnType(new QName(soapaction, method), String.class); // 要返回的数据类型(String类型)
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapaction + method);
info = (String) call.invoke(new Object[]{usertoken, countryType});// 调用方法并传递参数-传递的参数和设置的参数要对应,顺序不能搞错了
return info;
}
}