2011-05-13 65 views
0

我正在使用WebServices使用SOA。 所以我需要發送一個XML請求來接收另一個XML與響應。Java:如何發佈此XML請求?

我創建這個類:

package com.ws.test; 

import java.net.*; 
import java.io.*; 

public class SendXML { 

    public static void main(String[] args) { 

     try { 
      String strSOAP = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 

      strSOAP += "<SOAP-ENV:Envelope "; 

      strSOAP += " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/"; 

      strSOAP += " xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/"; 

      strSOAP += " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance"; 

      strSOAP += " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema"; 

      strSOAP += " xmlns:ns=\"urn:bacnet_ws\">"; 

      strSOAP += " <SOAP-ENV:Body SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"; 

      strSOAP += " <ns:getValue>"; 

      strSOAP += " <ns:options></ns:options>"; 

      strSOAP += " <ns:path>/.sysinfo/.vendor-name</ns:path>"; 

      strSOAP += " </ns:getValue>"; 

      strSOAP += " </SOAP-ENV:Body>"; 

      strSOAP += "</SOAP-ENV:Envelope>"; 


      //Create socket 
      String hostname = "192.168.1.2"; 
      int port = 8080; 
      InetAddress addr = InetAddress.getByName(hostname); 
      Socket sock = new Socket(addr, port); 

      //Send header 
      String path = "/rcx-ws/rcx"; 
      BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(), "UTF-8")); 
      // You can use "UTF8" for compatibility with the Microsoft virtual machine. 
      wr.write("POST " + path + " HTTP/1.0\r\n"); 
      wr.write("Host: 192.168.1.2\r\n"); 
      wr.write("Content-Length: " + strSOAP.length() + "\r\n"); 
      wr.write("Content-Type: text/xml; charset=\"utf-8\"\r\n"); 
      wr.write("\r\n"); 

      //Send data 
      wr.write(strSOAP); 
      wr.flush(); 

      // Response 
      BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream())); 
      String line; 
      while ((line = rd.readLine()) != null) { 

       System.out.println(line); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

但是,這是不對的,因爲我收到此消息錯誤:

HTTP/1.1 500 Internal Server Error Server: gSOAP/2.7 Content-Type: text/xml; charset=utf-8 Content-Length: 570 Connection: close

SOAP-ENV:VersionMismatchSOAP version mismatch or invalid SOAP message

我需要這個參數發送到另一個系統:

strSOAP += " <ns:getValue>"; 

strSOAP += " <ns:options></ns:options>"; 

strSOAP += " <ns:path>/.sysinfo/.vendor-name</ns:path>"; 

strSOAP += " </ns:getValue>"; 

我怎麼能在Java中做到這一點?

此致敬禮, 瓦爾特恩裏克。

+0

這是一種使用Web服務的瘋狂方式。您確實需要使用適當的Web服務客戶端框架,而不是張貼手工製作的XML字符串。 – skaffman 2011-05-13 11:06:17

回答

1

如果您堅持使用套接字傳輸數據,則必須查看SOAP specifications。例如,SOAP 1.1需要SOAPAction HTTP header。在開始編寫代碼之前,使用諸如soapUI之類的工具來檢查請求的有效性。

一般來說,使用現有的客戶端API(其中有幾個可供選擇)會更好。例如,您可以輕鬆使用hand-crafted soap requests with JAX-WS

有兩點要注意:


代碼泄漏資源。 Close您的流。請參閱try/finally pattern


BufferedWriter wr = new BufferedWriter(
        new OutputStreamWriter(sock.getOutputStream(), "UTF-8")); 
//... 
wr.write("Content-Length: " + strSOAP.length() + "\r\n"); 

這不會是用於代碼點上方U + 007F作爲String.length()返回的代碼單元中UTF-16的數量是安全的。 Content-Length應包含在byte s中的長度;不是Java char s。

+0

感謝您的幫助夥計 – 2011-05-13 23:03:09

1

好主人花花公子。不要過分諷刺,但你沒有聽說過JAX-WSJAX-RS?甚至不起眼,但可靠JAX-RPC?如果你真的想以這樣的舊石器時代的方式手動構建Web服務請求,那麼你如何才能開發基於Web服務的SOA?

我的意思是,來吧,我知道我們不應該告訴人們「谷歌它」,但嚴重的是,我們是否想到過你來嘗試之前做的Web服務調用例如「一個谷歌是編碼瘋狂?這是2011年,而不是1994-95年,當時大多數人都不知道網絡搜索是什麼。

我建議你從Java EE 5 JAX-WS tutorial開始,因爲你有很多東西需要學習。祝你好運。

+1

感謝您的評論Luis,也沒有人告訴JAX-WS或其他關於你的事情,所以我請求幫助。無論如何感謝 。 – 2011-05-13 23:02:44