`

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

 

分享到:
评论

相关推荐

    Java How to Program, Early Objects, 11th Edition, Deitel.pdf

    ### Java How to Program, Early Objects, 11th Edition #### 核心知识点概览 本书《Java How to Program, Early Objects, 11th Edition》由Deitel出版社出版,是一本面向初学者和有一定编程基础的学习者介绍Java...

    Java How to Program

    《Java How to Program》是一本深入浅出介绍Java编程语言的经典教材,由Deitel & Associates, Inc.出版。该书采用独特的“实战代码”方法,提供了大量的实际运行代码示例,帮助读者更好地理解和掌握Java编程的基本...

    Java大学教程(Java How to Program)(第四版)中文版 pdf

    本书以初学者为起点,循序渐进地介绍了面向对象的Java编程语言,系统地讨论了Java的基本概念和编程技术。全书共分为18章,首先从基本的Java理论开始,讲解了Java的基本数据类型和控制结构,Java中的方法、数组和字符...

    Java How To Program (late objects)

    ### Java How to Program (Late Objects) - 关键知识点解析 #### 一、书籍概述 《Java How to Program》(第十版,对象延迟版本)是一本经典的计算机编程教材,旨在为初学者提供系统全面的Java语言学习指导。本书...

    How to createavoid memory leak in Java and.NET.doc

    java

    Java How to Program 9th

    《Java How to Program 9th》是一本专为初学者和有一定基础的程序员设计的Java编程教程,旨在全面深入地介绍Java编程语言。这个版本是该书的第九版,更新了最新的Java SE 11内容,确保读者能够掌握当前最前沿的编程...

    Prentice.Hall.Java.How.to.Program.9th.Edition

    《Java How to Program》第九版不仅仅是一本教授Java编程技巧的书籍,它还将编程技能与解决社会问题相结合。通过这本书,读者可以学习如何使用Java来编写与上述社会问题相关的应用程序。这种结合使得学生不仅能够...

    How to invoke Java web service in ASP

    How to invoke Java web service in ASP

    Think Java: How to Think Like a Computer Scientist [2016]

    Think Java: How to Think Like a Computer Scientist by Allen B. Downey, Chris Mayfield 2016 | ISBN: 1491929561 Currently used at many colleges, universities, and high schools, this hands-on ...

    javahow to program ppt

    教材是java how to program,这是第一二章的英文ppt课件

    Java How to Program, Early Objects, 11th Edition

    Java How to Program, Early Objects, 11th Edition Java How to Program, Early Objects, 11th Edition Java How to Program, Early Objects, 11th Edition Java How to Program, Early Objects, 11th Edition

    linux howto中文合集

    这个是合集~一般是这里最全的了 另外加的 PDF是英文的 .为 GCC4.1的HOWTO &lt;br&gt;目录 &lt;br&gt;ADSM-Backup-html&lt;br&gt;Advocacy&lt;br&gt;Automount-html&lt;br&gt;Battery-Powered-html&lt;br&gt;BootPrompt-HOWTO&lt;br&gt;Bridge-......

    how to set for OpenMP in Visual Studio 2005

    It introduces how to setup for OpenMP in Visual Studio 2005 with Inter Fortran 10.1. With a simple 'Hello world ' example

    Java How to Program(9th Edition)

    《Java How to Program》是Paul Deitel和Harvey Deitel两位作者合著的一本经典Java编程教程,已更新至第九版。这本书旨在帮助初学者掌握Java编程语言,并为有经验的程序员提供深入学习的资源。在第九版中,作者们...

Global site tag (gtag.js) - Google Analytics