java 调用第三方webservice接口
时间: 2023-11-28 08:05:42 浏览: 44
以下是Java调用第三方webservice接口的两种方法:
1.使用AXIS调用远程的web service
```java
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.net.URL;
public class WebServiceClient {
public static void main(String[] args) throws Exception {
String endpoint = "http://www.example.com/webservice";
String namespace = "http://www.example.com/webservice";
String method = "getWeather";
String soapActionURI = "http://www.example.com/webservice/getWeather";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(endpoint));
call.setOperationName(new QName(namespace, method));
call.setSOAPActionURI(soapActionURI);
String result = (String) call.invoke(new Object[] { "Beijing" });
System.out.println(result);
}
}
```
2.利用wsdl2java工具生成webservice的客户端代码
首先,使用以下命令生成客户端代码:
```shell
java org.apache.axis.wsdl.WSDL2Java http://www.example.com/webservice?wsdl
```
然后,在生成的代码中调用webservice接口:
```java
import com.example.webservice.*;
public class WebServiceClient {
public static void main(String[] args) throws Exception {
GlobalWeatherSoapStub stub = new GlobalWeatherSoapStub();
String result = stub.getWeather("Beijing", "China");
System.out.println(result);
}
}
```