package hanwl.TestDemo;
import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map;
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.io.IOUtils; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.xml.sax.InputSource;
public class TestWebservice2 {
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> IOException { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TODO Auto-generated method stub</span> String wsdl = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl"<span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">int</span> timeout = 1000<span style="color: rgba(0, 0, 0, 1)">; StringBuffer sb </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> StringBuffer(""<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"<?xml version="1.0" encoding="UTF-8"?>"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"<soapenv:Envelope " + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " + " xmlns:q0='http://WebXml.com.cn/' " + " xmlns:xsd='http://www.w3.org/2001/XMLSchema' " + " xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' >"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"<soapenv:Body>"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"<q0:getWeatherbyCityName>"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"<q0:theCityName>唐山</q0:theCityName> "<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"</q0:getWeatherbyCityName>"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"</soapenv:Body>"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"</soapenv:Envelope>"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> HttpClient发送SOAP请求</span> System.out.println("HttpClient 发送SOAP请求"<span style="color: rgba(0, 0, 0, 1)">); HttpClient client </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> HttpClient(); PostMethod postMethod </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> PostMethod(wsdl); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置连接超时</span>
client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置读取时间超时</span>
client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 然后把Soap请求数据添加到PostMethod中</span> RequestEntity requestEntity = <span style="color: rgba(0, 0, 255, 1)">new</span> StringRequestEntity(sb.toString(), "text/xml", "UTF-8"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置请求头部,否则可能会报 “no SOAPAction header” 的错误 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">postMethod.setRequestHeader("SOAPAction",""); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 设置请求体</span>
postMethod.setRequestEntity(requestEntity);
</span><span style="color: rgba(0, 0, 255, 1)">int</span> status =<span style="color: rgba(0, 0, 0, 1)"> client.executeMethod(postMethod); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打印请求状态码</span> System.out.println("status:" +<span style="color: rgba(0, 0, 0, 1)"> status); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取响应体输入流</span> InputStream is =<span style="color: rgba(0, 0, 0, 1)"> postMethod.getResponseBodyAsStream(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取请求结果字符串</span> String result =<span style="color: rgba(0, 0, 0, 1)"> IOUtils.toString(is); Document dc </span>=<span style="color: rgba(0, 0, 0, 1)"> strXmlToDocument(result);
// Element root = dc.getRootElement(); // System.out.println(root.getName()); // System.out.println(“result: ” + result);
} </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> Document strXmlToDocument(String parseStrXml){ Document document </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> { document </span>=<span style="color: rgba(0, 0, 0, 1)"> DocumentHelper.parseText(parseStrXml); Element root </span>=<span style="color: rgba(0, 0, 0, 1)"> document.getRootElement(); List</span><Element> list =<span style="color: rgba(0, 0, 0, 1)"> root.elements(); getElement(list); } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (DocumentException e) { e.printStackTrace(); } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> document; } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> getElement(List<Element><span style="color: rgba(0, 0, 0, 1)"> sonElemetList) {
// Map<String,String> map = new HashMap<String, String>();
<span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Element sonElement : sonElemetList) { </span><span style="color: rgba(0, 0, 255, 1)">if</span> (sonElement.elements().size() != 0<span style="color: rgba(0, 0, 0, 1)">) { System.out.println(sonElement.getName() </span>+ ":"<span style="color: rgba(0, 0, 0, 1)">); getElement(sonElement.elements()); }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">{ System.out.println(sonElement.getName() </span>+ ":"+<span style="color: rgba(0, 0, 0, 1)"> sonElement.getText()); } } }
}
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/11259.html