`

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/"

 

分享到:
评论

相关推荐

    How to Use JS Calling A WebService By Post Both SOAP And JSON

    xhr.send(soapMessage); ``` 在这个例子中,我们创建了一个新的XMLHttpRequest对象,设置请求方法为POST,目标URL为SOAP服务地址,并指定了Content-Type为XML。然后,我们构建了一个SOAP消息,并在准备好后发送请求...

    How to Get Notifications from a .NET Web Service

    public void SendNotification(string message) { // 实现发送通知的逻辑 } } ``` 2. 实现通知发送: - 在`SendNotification`方法中,可以编写代码来处理实际的通知发送,这可能涉及到数据库操作、邮件发送...

    php.ini-development

    in production environments and one that is recommended to be used in ; development environments. ; php.ini-production contains settings which hold security, performance and ; best practices at its ...

    新目标英语八年级下册第四单元词组精选.doc

    4. **向…问好** (Send one's love to sb., Give one's regards to sb.): 这是表达对他人问候的方式,通常用于信件或对话中。 5. **期末考试** (End-of-year exams): 学期结束时进行的评估学生学习成果的重要测试。...

    ZendFramework中文文档

    1. Introduction to Zend Framework 1.1. 概述 1.2. 安装 2. Zend_Acl 2.1. 简介 2.1.1. 关于资源(Resource) 2.1.2. 关于角色(Role) 2.1.3. 创建访问控制列表(ACL) 2.1.4. 注册角色(Role) 2.1.5. 定义访问...

Global site tag (gtag.js) - Google Analytics