public static String doRequest(String url,String method,String data,String xmlns,String way) {
String result="";
try {
// 2 创建URL:
StringBuffer sb=null;
URL req_url = new URL(url);
//第二步:打开一个通向服务地址的连接
HttpURLConnection con= (HttpURLConnection) req_url.openConnection();
//con.setConnectTimeout(30000);// 设置连接主机超时时间(毫秒)
//con.setReadTimeout(30000);// 设置主机读取数据超时
// 4,设置请求方式和请求头:
con.setDoInput(true); // 是否有入参
con.setDoOutput(true); // 是否有出参
con.setRequestMethod(method); // 设置请求方式
con.setRequestProperty("connection", "Keep-Alive");
con.setRequestProperty("user-agent", "Apache-HttpClient/4.1.1");
//con.setRequestProperty("Accept-Encoding", "gzip,deflate");
con.setRequestProperty("content-type", "text/xml;charset=utf-8");
//设置请求参数
String requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"";
requestBody += " xmlns:web=\""+xmlns+"\">";
requestBody += "<soapenv:Body>";
requestBody += "<web:"+way+">";
requestBody += "<web:in0>";
requestBody +="<![CDATA["+data+"]]>";
requestBody += "</web:in0>";
requestBody += "</web:"+way+">";
requestBody += "</soapenv:Body>";
requestBody += "</soapenv:Envelope>";
new BaseBean().writeLog("请求包体--", requestBody);
//System.out.println("请求包体--"+ requestBody);
// 6,通过流的方式将请求体发送出去:
OutputStream out = con.getOutputStream();
out.write(requestBody.getBytes("utf-8"));
out.flush();
out.close();
// 7,服务端返回正常:
int code = con.getResponseCode();
//System.out.println(code);
new BaseBean().writeLog("code--", code);
if (code == 200) {// 服务端返回正常
InputStream is = con.getInputStream();
byte[] b = new byte[1024];
sb = new StringBuffer();
int len = 0;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len, "UTF-8");
sb.append(str);
}
result=sb.toString();
//System.out.println(result);
is.close();
con.disconnect();
}
/*
* else{
System.out.println("服务器返回失败");
}
*/
}catch (Exception e) {
System.out.println(e.getMessage());
}
return result;
}
//w3c dom解析soap
public static String GetSoap(String result) throws Exception{
//解析Soap包文
//new BaseBean().writeLog("获取返回数据"+result);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new InputSource(new StringReader(result)));
NodeList sessions = document.getChildNodes();
String xml="";
for (int i = 0; i < sessions.getLength(); i++) {
Node session = sessions.item(i);
NodeList sessionInfo = session.getChildNodes();
for (int j = 0; j < sessionInfo.getLength(); j++) {
Node node = sessionInfo.item(j);
NodeList sessionMeta = node.getChildNodes();
for (int k = 0; k < sessionMeta.getLength(); k++) {
xml=sessionMeta.item(k).getTextContent();
}
}
}
System.out.println("获取xml 文件"+xml);
return xml;
}
// XML格式字符串转hashmap通过key value 获取指定标签值
public static Map<String, String> getXMLStringValue(String result){
Map<String, String> rm = new HashMap<String, String>();
StringReader sr = new StringReader(result); // result数据源:XML格式字符串
InputSource is = new InputSource(sr);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
org.w3c.dom.Document document = null;
try {
document = builder.parse(is);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Element rootElement = document.getDocumentElement();
NodeList nodes = rootElement.getChildNodes(); // 一级标签
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.getFirstChild() != null) {
rm.put(node.getNodeName(), node.getFirstChild().getNodeValue());
}
NodeList nodeList = node.getChildNodes(); // 二级标签
if (nodeList != null) {
for (int j = 0; j < nodeList.getLength(); j++) {
Node childnode1 = nodeList.item(j);
if (childnode1 instanceof Element) {
if (childnode1.getFirstChild() != null) {
rm.put(childnode1.getNodeName(), childnode1.getFirstChild().getNodeValue());
}
NodeList nodeList1 = childnode1.getChildNodes(); // 三级标签
if (nodeList1 != null) {
for (int k = 0; k < nodeList1.getLength(); k++) {
Node childnode2 = nodeList1.item(k);
if (childnode2 instanceof Element) {
if (childnode2.getFirstChild() != null) {
rm.put(childnode2.getNodeName(), childnode2.getFirstChild().getNodeValue());
}
NodeList nodeList2 = childnode2.getChildNodes();
// 当有四级或以上标签时,在此重复写解析代码
}
}
}
}
}
}
}
}
return rm;
}
}
在main里面调用测试,这里只贴出调用代码
try {
//准备xml参数
String xml="xml参数";
String data = SmsWebserviceUtil.doRequest("接口地址", "请求方式",xml, "http://webservice","接口方法");
//System.out.println(data);
String result=SmsWebserviceUtil.GetSoap(data);
Map<String,String> requestMap=SmsWebserviceUtil.getXMLStringValue(result);
// 获取指定标签值
String value=requestMap.get("key");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
解析soap这两个方法可以优化合并。大佬们可以合并下。