i’m working with SOA using WebServices.
So i need to send a XML request to receive another XML with the response.
I create this class:
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();
}
}
}
But this is not right because i receive this message error:
HTTP/1.1 500 Internal Server Error
Server: gSOAP/2.7 Content-Type:
text/xml; charset=utf-8
Content-Length: 570 Connection: closeSOAP-ENV:VersionMismatchSOAP
version mismatch or invalid SOAP
message
I need to send this parameter to the another system :
strSOAP += " <ns:getValue>";
strSOAP += " <ns:options></ns:options>";
strSOAP += " <ns:path>/.sysinfo/.vendor-name</ns:path>";
strSOAP += " </ns:getValue>";
How i could do that in Java ?
Best regards,
Valter Henrique.
If you insist on using sockets to transmit data, you’ll have to look at the SOAP specifications. For example, SOAP 1.1 requires the SOAPAction HTTP header. Use a tool like soapUI to check the validity of requests before you start writing code.
In general, you’d be better off using an existing client API (of which there are a few to choose). For example, you can easily use hand-crafted soap requests with JAX-WS.
A couple of notes:
The code leaks resources. Close your streams. See the try/finally pattern.
This would not be safe for code points above U+007F as
String.length()returns the number of code units in UTF-16.Content-Lengthshould contain the length inbytes; not Javachars.