`

how to send soap message in java

阅读更多
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

public class PostXml {

	public static void main(String[] args) {

		String message = "<?xml version=\"1.0\" encoding=\"utf-8\"?><TaskManagerMessage><MessageID>71694280-28e0-4314-8351-f1793be7eef5</MessageID><MessageType>RequestDeptListQuery</MessageType><IsResponse>false</IsResponse><MessageSendTime>0001-01-01T00:00:00</MessageSendTime><Params><DeptType>1</DeptType><CurrentPage>1</CurrentPage><PageRows>10</PageRows></Params></TaskManagerMessage>";
		
		message = message.replace("<", "&lt;").replace(">", "&gt;");
		
		try {
			String xmldata = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
					+ "<env:Envelope "
					+ "env:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" "
					+ "xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\" "
					+ "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
					+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
					+ "<env:Header/>"
					+ "<env:Body>"
					+ "<WebserviceCrud xmlns=\"http://tempuri.org/\">"
					+ "<strxml>" + message + "</strxml>"
					+ "</WebserviceCrud>" + "</env:Body>" + "</env:Envelope>";

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

			// Send header
			String path = "/webservice/Service.asmx";
			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.1\r\n");
			wr.write("Host: " + hostname + "\r\n");
			wr.write("Content-Length: " + xmldata.length() + "\r\n");
			wr.write("Content-Type: text/xml; charset=\"utf-8\"\r\n");
			wr.write("\r\n");

			// Send data
			wr.write(xmldata);
			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();
		}
	}
}
 

and here, you may find there's a node which has env as its part, is it a set one? no, it's not a set one, it's a self-defined name, like this:

 

String xmldata = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
					+ "<soap:Envelope "
					+ "soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" "
					+ "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" "
					+ "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
					+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
					+ "<soap:Header/>"
					+ "<soap:Body>"
					+ "<WebserviceCrud xmlns=\"http://tempuri.org/\">"
					+ "<strxml>" + message + "</strxml>"
					+ "</WebserviceCrud>" + "</soap:Body>" + "</soap:Envelope>";
 

change it to "soap", we can make it go too.

here's the essential:

xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics