我正在使用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中做到這一點?
此致敬禮, 瓦爾特恩裏克。
這是一種使用Web服務的瘋狂方式。您確實需要使用適當的Web服務客戶端框架,而不是張貼手工製作的XML字符串。 – skaffman 2011-05-13 11:06:17