public class WebUtil {
public static String service(String xml,String op,String surl) throws IOException {
// 需要调用的方法
String soapActionString = "http://tempuri.org/"+op;
//第一步:创建服务地址,不是WSDL地址
//URL url = new URL("http://IP地址/WebService.asmx");
URL url = new URL(surl);
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 拼接soap
String soap = xml;
byte[] buf = soap.getBytes("UTF-8");
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
//3.3设置SOPEAction
connection.setRequestProperty("soapActionString", soapActionString);
//3.4设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);
OutputStream out = connection.getOutputStream();
out.write(buf);
out.close();
//第4步:接收服务端响应,打印(xml格式数据)
int responseCode = connection.getResponseCode();
StringBuffer sb = new StringBuffer();
if (200 == responseCode) {
InputStream is = connection.getInputStream();
byte[] b = new byte[1024];
int len = 0;
while ((len = is.read(b)) != -1) {
String s = new String(b, 0, len, "utf-8");
sb.append(s);
}
is.close();
}
return sb.toString();
}
/**
* <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getMobileCodeInfo xmlns="http://WebXml.com.cn/">
<mobileCode>string</mobileCode>
<userID>string</userID>
</getMobileCodeInfo>
</soap:Body>
</soap:Envelope>
* @param phoneNum
* @return
*/
public static void main(String[] args) throws IOException {
//传入的参数 具体的 看你的接口
String soapXML = "<?xml version='1.0' encoding='utf-8'?>"
+"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"
+"<soap:Body>"
+"<GetDevicaInfoByParameter xmlns='http://tempuri.org/'>"
+"<usercode>用户名</usercode>"
+"<userpassword>密码</userpassword>"
+"<param>"
+"<FADD>2640FC1418438016</FADD>"
+"<FADD_Name>xxxxx</FADD_Name>"
+"</param>"
+"<pageIndex>1</pageIndex>"
+"<rowsCount>1</rowsCount>"
+"</GetDevicaInfoByParameter>"
+"</soap:Body>"
+"</soap:Envelope>";
String service= service(soapXML,"GetDevicaInfoByParameter","http://ip地址/WebService.asmx");
jxXml(service);
System.out.println(cl6);
}
private static void jxXml(String service) {
try {
// 将字符串转为XML
Document doc = DocumentHelper.parseText(service);
//获取文档的根节点
Element rootElt = doc.getRootElement(); // 获取根节点
//System.out.println("根节点:"+rootElt); //根节点名称 Envelope
//获取子body的子节点的子节点
Element element = rootElt.element("Body").element("GetDevicaInfoByParameterResponse").element("GetDevicaInfoByParameterResult");
//获取所有DeviceDataModel下的值
List<Element> elements = element.element("Data").elements("DeviceDataModel");
//循环 elements
for (Element element2 : elements) {
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
用的是dom4j解析 xml
本文介绍了一种使用Java进行WebService调用的方法,通过SOAP协议发送请求,并利用DOM4J库解析返回的XML数据。示例代码展示了如何构造SOAP请求,设置HTTP连接属性,以及如何从服务端接收并解析响应。

6012

被折叠的 条评论
为什么被折叠?



