方式一:利用AXIS调用远程的web service
import java.net.URL;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class Client {
public static void main(String[] args) throws Exception {
Call call=(Call)(new Service()).createCall();
//URL中为webService暴露的接口地址
call.setTargetEndpointAddress(new URL("http://IP:端口/XXX"));
// namespace 为命名空间 方法名即为你要调用的方法
call.setOperationName(new QName("namespace","方法名"));
call.setTimeout(30000);
// 参数 是你调用方法时传的参数
String re=(String)call.invoke(new String[]{参数});
System.out.println(re);
}
}
Maven依赖:
<!-- axis 1.4 jar start -->
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-discovery</groupId>
<artifactId>commons-discovery</artifactId>
<version>0.2</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-jaxrpc</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis-saaj</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- axis 1.4 jar end -->
方式二:利用CXF调用WebService接口
public static void main (String[] args){
try{
String webServiceUrl = "http://238.62.169.140:8090/g6_venues/webservice/gtv-external-venues-service?wsdl";
JaxWsDynamicClientFactory factory1 = JaxWsDynamicClientFactory.newInstance();
Client client = factory1.createClient(webServiceUrl);
HTTPConduit conduit = (HTTPConduit)client.getConduit();
HTTPClientPolicy clientPolicy = new HTTPClientPolicy();
//连接超时
clientPolicy.setConnectionTimeout(2000);
//取消块编码
clientPolicy.setAllowChunking(false);
//响应超时
clientPolicy.setReceiveTimeout(12000);
conduit.setClient(clientPolicy);
String startDateTime = "20201018090000";
String endDateTime = "20201228160000";
//("方法名","参数1","参数2",......)
Object[] result = client.invoke("queryFreeRoomInfo",startDateTime ,endDateTime ); //queryFreeRoomInfo为方法名;
System.out.println("返回结果:" + result[0].toString());
}catch (Exception e){
e.printStackTrace();
}
}
Maven依赖:
JaxWsDynamicClientFactory、HTTPConduit、Client、HTTPClientPolicy需要引入对应的依赖,
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.10</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.10</version>
</dependency>
总结:两种方式都可调通webservice接口,但是重要的还是要把webservice生成的xml文件搞明白才行哦。