webservice實現有多種方式
比如最常用的有axis架構,xfire架構,通過該架構發行就緒wsdl介面,也可以實現webservice用戶端,目前eclipse都有整合的外掛程式,可以根據wsdl檔案產生webservice用戶端調用介面,但是這樣部署的時候必須依賴架構的jar包,有時候可能因為環境等等原因,我們僅僅需要wsdl中的某一個介面,這時候可以通過http介面或socket介面直接發生xml資料,來調用服務端webservice服務,其實webservice底層還是發送xml資料,只是架構封裝了對xml資料進行序列化與還原序列化操作,下面以兩個簡單的例子說明http方式和socket方式。
http實現webservice介面調用例子:
import java.io.BufferedReader;<br />import java.io.IOException;<br />import java.io.InputStreamReader;<br />import java.io.OutputStreamWriter;<br />import java.io.UnsupportedEncodingException;<br />import java.net.MalformedURLException;<br />import java.net.URL;<br />import java.net.URLConnection;</p><p>public class HttpPostTest {<br /> void testPost(String urlStr) {<br /> try {<br /> URL url = new URL(urlStr);<br /> URLConnection con = url.openConnection();<br /> con.setDoOutput(true);<br /> con.setRequestProperty("Pragma:", "no-cache");<br /> con.setRequestProperty("Cache-Control", "no-cache");<br /> con.setRequestProperty("Content-Type", "text/xml");</p><p> OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());<br /> String xmlInfo = getXmlInfo();<br /> out.write(new String(xmlInfo));<br /> out.flush();<br /> out.close();<br /> BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));<br /> String line = "";<br /> StringBuffer buf = new StringBuffer();<br /> for (line = br.readLine(); line != null; line = br.readLine()) {<br /> buf.append(new String(line.getBytes(),"UTF-8"));<br /> }<br /> System.out.println(buf.toString());<br /> } catch (MalformedURLException e) {<br /> e.printStackTrace();<br /> } catch (IOException e) {<br /> e.printStackTrace();<br /> }<br /> }</p><p> private String getXmlInfo() {<br /> // 通過wsdl檔案可以查看介面xml格式資料,構造調用介面xml資料<br /> String xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=/"http://schemas.xmlsoap.org/soap/envelope//" xmlns:SOAP-ENC=/"http://schemas.xmlsoap.org/soap/encoding//" xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/">"<br /> + "<SOAP-ENV:Body>"<br /> + "<m:getItemDetailSingle xmlns:m=/"http:xxxxxxxxxxxxxxxxxx//">"<br /> + "<itemMo>"<br /> + "<category>工廠類</category>"<br /> + "<city>北京</city>"<br /> + "<flag>1</flag>"<br /> + "<itemId>0</itemId>"<br /> + "<itemIndex>1</itemIndex>"<br /> + "<keyword></keyword>"<br /> + "<mobile>2147483647</mobile>"<br /> + "<password>123456</password>"<br /> + "<userName>sohu</userName>"<br /> + "</itemMo>"<br /> + "</m:getItemDetailSingle>"<br /> + "</SOAP-ENV:Body>"<br /> + "</SOAP-ENV:Envelope>";<br /> return xml;<br /> }</p><p> public static void main(String[] args) throws UnsupportedEncodingException {<br /> String url = "http://localhost:9003/dataService/services/Job";<br /> new HttpPostTest().testPost(url);<br /> }<br />}<br />
socke方式實現例子:
import java.io.IOException;<br />import java.io.InputStream;<br />import java.io.InputStreamReader;<br />import java.io.OutputStream;<br />import java.net.Socket;<br />import java.net.UnknownHostException;</p><p>public class WebServiceClient {</p><p> /**<br /> * @param args<br /> * @throws IOException<br /> * @throws UnknownHostException<br /> */<br /> public static void main(String[] args) throws UnknownHostException, IOException {<br /> Socket socket = new Socket("localhost",9003);<br /> OutputStream os = socket.getOutputStream();<br /> InputStream is = socket.getInputStream();<br /> //System.out.println(socket.isConnected());<br /> String httpSend = "POST /dataService/services/Job HTTP/1.1/r/n"<br /> + "Content-Type:text/xml/r/n"<br /> + "Host:localhost:9003/r/n"<br /> + "Content-Length:1024/r/n"<br /> + "/r/n"<br /> + "<SOAP-ENV:Envelope xmlns:SOAP-ENV=/"http://schemas.xmlsoap.org/soap/envelope//" xmlns:SOAP-ENC=/"http://schemas.xmlsoap.org/soap/encoding//" xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/">"<br /> + "<SOAP-ENV:Body>"<br /> + "<m:getItemDetailSingle xmlns:m=/"http://localhost//">"<br /> + "<itemMo>"<br /> + "<category>工廠類</category>"<br /> + "<city>北京</city>"<br /> + "<flag>1</flag>"<br /> + "<itemId>0</itemId>"<br /> + "<itemIndex>1</itemIndex>"<br /> + "<keyword>String</keyword>"<br /> + "<mobile>2147483647</mobile>"<br /> + "<password>123456</password>"<br /> + "<userName>sohu</userName>"<br /> + "</itemMo>"<br /> + "</m:getItemDetailSingle>"<br /> + "</SOAP-ENV:Body>"<br /> + "</SOAP-ENV:Envelope>";<br /> os.write(httpSend.getBytes());<br /> os.flush(); </p><p> InputStreamReader ireader = new InputStreamReader(is);<br /> java.io.BufferedReader breader = new java.io.BufferedReader(ireader); </p><p> String responseLine = ""; </p><p> while((responseLine = breader.readLine()) != null)<br /> {<br /> System.out.println(new String(responseLine.getBytes(),"UTF-8"));<br /> } </p><p> System.out.println(""); </p><p> }</p><p>}<br />