当前位置:网站首页 > Java基础 > 正文

java soap webservice教程



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>"&lt;?xml version="1.0" encoding="UTF-8"?&gt;"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"&lt;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/' &gt;"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"&lt;soapenv:Body&gt;"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"&lt;q0:getWeatherbyCityName&gt;"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"&lt;q0:theCityName&gt;唐山&lt;/q0:theCityName&gt; "<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"&lt;/q0:getWeatherbyCityName&gt;"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"&lt;/soapenv:Body&gt;"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"&lt;/soapenv:Envelope&gt;"<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>&lt;Element&gt; 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&lt;Element&gt;<span style="color: rgba(0, 0, 0, 1)"> sonElemetList) { 

// Map&lt;String,String&gt; map = new HashMap&lt;String, String&gt;();

 <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()); } } } 

}

  • 上一篇: java必学教程
  • 下一篇: java教程163
  • 版权声明


    相关文章:

  • java必学教程2025-12-03 17:26:01
  • solr java 安装教程2025-12-03 17:26:01
  • java分库教程2025-12-03 17:26:01
  • java教程之io流2025-12-03 17:26:01
  • java伺服器创建教程2025-12-03 17:26:01
  • java教程1632025-12-03 17:26:01
  • java程序设计教程 雍俊海 答案2025-12-03 17:26:01
  • java项目包制作教程2025-12-03 17:26:01
  • java教程epub2025-12-03 17:26:01
  • java中awt教程2025-12-03 17:26:01