一、生成ws client代码调用
该种方式使用简单,但一些关键的元素在代码生成时写死到生成代码中,不方便维护,所以仅用于测试。
二、service编程调用
该种方式可以自定义关键元素,方便以后维护,是一种标准的开发方式
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
public class ServiceClient {
public static void main(String[] args) throws IOException {
//创建WSDL的URL,注意不是服务地址
URL url = new URL("http://localhost:8080/ws/hello?wsdl");
//创建服务名称
//1.namespaceURI - 命名空间地址
//2.localPart - 服务视图名
QName qname = new QName("http://WebXml.com.cn/", "MobileCodeWS");
//创建服务视图
//参数解释:
//1.wsdlDocumentLocation - wsdl地址
//2.serviceName - 服务名称
Service service = Service.create(url, qname);
//获取服务实现类
WsCheckService wsCheckService = service.getPort(WsCheckService .class);
//调用查询方法
String result = wsCheckService.check();
System.out.println(result);
}
}
三、HttpURLConnection调用方式
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
*
* <p>Title: HttpClient.java</p>
* <p>Description:HttpURLConnection调用方式</p>
*/
public class HttpClient {
public static void main(String[] args) throws IOException {
//第一步:创建服务地址,不是WSDL地址
URL url = new URL("http://localhost:8080/ws/hello");
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);
//第四步:组织SOAP数据,发送请求
String soapXML = getXML("15226466316");
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
if(200 == responseCode){//表示服务端响应成功
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp = null;
while(null != (temp = br.readLine())){
sb.append(temp);
}
System.out.println(sb.toString());
is.close();
isr.close();
br.close();
}
os.close();
}
/** 拼装ws 请求xml, 可从soapUI得到*/
public static String getXML(String xxx){
String soapXML = "";
return soapXML;
}
}
四、client代码 + spring
1、生成client代码
2、配置spring bean
<jaxws:client id="checkClient" serviceClass="com.webservice.check.WsCheckService" address="http://localhost:8080/ws/hello?wsdl" />
五、cxf 代理
public String webserviceRequest(String bidCode){
try {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.setAddress("http://localhost:8080/ws/hello?wsdl");
factoryBean.setServiceClass(HelloService.class);//通过接口指定请求方法名称/返回类型/参数
HelloService hello =(HelloService)factoryBean.create();
configTimeout(hello);//超时设置
String result = ex.check();//请求完毕后、类型接收
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 客户端调用请求时超时设置
* @param service
*/
public static void configTimeout(Object service) {
Client proxy = ClientProxy.getClient(service);
HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(8*1000);//8S 请求时间
policy.setReceiveTimeout(10*1000);//10S 连接时间
conduit.setClient(policy);
}
六、soapUI调用
1、新建项目,填写wsdl URL后下一步下一步完成!
2、然后选择测试方法,填写表单
3、发送