private Integer CONN_TIMEOUT = 10 * 1000; // 连接超时时间ms
private Integer RECV_TIMEOUT = 20 * 1000; // 接收超时时间ms
// 命名空间及调用方法名称
QName qName = new QName("http://WebXml.com.cn/", "getCountryCityByIp");
// 参数列表,多个参数逗号隔开
Object[] parameters = { theIpAddress };
// webservice 带参地址
Stirng url = "http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?param1=xxx¶m2=xxx";
public static String wsInvoke(String url, QName qName, Object[] params) {
String res = "";
JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
try {
String wsdl = url.substring(0,url.indexOf("?"));
// 使用 wsdl 地址生成客户端
Client client = factory.createClient(wsdl + "?wsdl");
// 修改客户端调用地址使调用带参数
client.getEndpoint().getEndpointInfo().setAddress(url);
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(CONN_TIMEOUT);
policy.setReceiveTimeout(RECV_TIMEOUT);
// ConnectionType.KEEP_ALIVE 保持长连接
// ConnectionType.CLOSE 请求处理完成关闭连接
policy.setConnection(ConnectionType.KEEP_ALIVE);
conduit.setClient(policy);
Object[] objects = client.invoke(qName, params);
res = objects[0].toString();
} catch (Exception e) {
e.printStackTrace();
}
return res;
}