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开发者_如何学C 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: close
SOAP-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.
BufferedWriter wr = new BufferedWriter(
new OutputStreamWriter(sock.getOutputStream(), "UTF-8"));
//...
wr.write("Content-Length: " + strSOAP.length() + "\r\n");
This would not be safe for code points above U+007F as String.length()
returns the number of code units in UTF-16. Content-Length
should contain the length in byte
s; not Java char
s.
Good lord dude. Not to be overly sarcastic, but haven't you heard of JAX-WS? JAX-RS? Or even the humble, but reliable JAX-RPC? How could you be working on web-service based SOA if you are actually trying to build a web service request by hand like that, in such a paleolithic way.
I mean, c'mon, I know we shouldn't tell people to 'google it', but seriously, did it ever occur to you to do a google on 'web service call example' before trying that coding madness? This is 2011, not 1994-95 when most people didn't know what a web search was.
I suggest you start with the Java EE 5 JAX-WS tutorial as there is much for you to learn. Good luck.
精彩评论