首先要得到webService请求方法的报文也就是soap
下载soapUI模拟请求,soapUi会自动根据wsdl地址生成我们所需要的xml
也就是这种
拿到这个文件后就是写代码了
用浏览器打开wsdl地址拿到
public static void main(String[] args) {
HttpClient client = new HttpClient();
/*soap:address location*/
PostMethod method = new PostMethod("");
client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);// 设置连接时间
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");/*设置字符编码*/
String soapRequestData = getRequestXml();//soap协议的格式,定义了方法和参数
try {
/*设置连接格式请求的xml*/
RequestEntity re = new StringRequestEntity(soapRequestData, "application/soap+xml", "utf-8");
method.setRequestEntity(re);
/*请求连接返回的状态 200请求成功*/
int state = client.executeMethod(method);
System.out.println(state);
/*拿到返回的数据此时返回的是一个soap的xml文件*/
String data = method.getResponseBodyAsString();
String js = parseXML(data);
System.out.println(js);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/*soapui生成的报文*/
private static String getRequestXml(){
StringBuilder sb = new StringBuilder();
sb.append("");
sb.append(" sb.append(" xmlns:mon=\"http://monitor.ws.platform.isoftstone.com/\">"); sb.append(" sb.append(" /*方法名*/ sb.append(" /*参数1*/ sb.append(" /*参数2*/ sb.append(" sb.append(""); sb.append(""); sb.append("
return sb.toString();
}
/*解析报文*/
public static String parseXML(String xmlString){
String result = "";
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
Element root = doc.getDocumentElement();//根节点
Node node = root.getFirstChild();
while(!node.getNodeName().equals("return")) {
node = node.getFirstChild();
}
if(node.getFirstChild() != null){
result = node.getFirstChild().getNodeValue();
}
System.out.println("获取的返回参数为:" + result);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}