SOAP消息的创建、传递和处理(MESSAGE方式)

private String ns = "http://service.soap.org/";
private String wsdlUrl = "http://localhost:8989/ms?wsdl";

/**
 * 消息的传递和处理(MESSAGE)
 * 提交给服务器
 */
@Test
public void test02() {
	try {
		//1.创建服务(Service)
		URL url = new URL(wsdlUrl);
		QName sName = new QName(ns, "MyServiceImplService");
		Service service = Service.create(url, sName);
		
		//2.创建Dispatch
		Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns, "MyServiceImplPort"),
				SOAPMessage.class, Service.Mode.MESSAGE);
		
		//3.创建SOAP消息
		//根据消息工厂创建SOAPMessage
		SOAPMessage message = MessageFactory.newInstance().createMessage();
		//通过SOAPPart获取SOAPEnvelope
		SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
		//通过SOAPEnvelope有效获取相应的Body和Header等信息
		SOAPBody body = envelope.getBody();
		
		//4.创建QName来指定消息中传递的数据
		//(QName就是一个带有命名空间的节点)
		QName eName = new QName(ns, "add", "nn");	//<nn:add xmlns="ns" />
		SOAPBodyElement element = body.addBodyElement(eName);
		element.addChildElement("a").setValue("22");
		element.addChildElement("b").setValue("33");
		message.writeTo(System.out);
		System.out.println("\n invoking......");
		
		/*
		 *上面步骤3、4即为SOAP消息体的创建,打印结果为: 
		 * <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
		 * 	<SOAP-ENV:Header/>
		 * 	<SOAP-ENV:Body>
		 * 		<nn:add xmlns:nn="http://service.soap.org/">
		 * 			<a>22</a>
		 * 			<b>33</b>
		 * 		</nn:add>
		 * 	</SOAP-ENV:Body>
		 * </SOAP-ENV:Envelope>
		 * invoking......
		 */

		//5.通过Dispatch传递信息,会返回响应消息
		SOAPMessage response = dispatch.invoke(message);
		response.writeTo(System.out);
		System.out.println();
		/*
		 * 上面步骤为向服务器端传递消息,打印结果为:
		 * <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
		 * 	<S:Header/>
		 * 	<S:Body>
		 * 		<ns2:addResponse xmlns:ns2="http://service.soap.org/">
		 * 			<addResult>55</addResult>
		 * 		</ns2:addResponse>
		 * 	</S:Body>
		 * </S:Envelope>
		 */
		
		//6.将响应的消息转换为dom对象(解析xml)
		Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
		String strResult = doc.getElementsByTagName("addResult").item(0).getTextContent();
		System.out.println(strResult);
		/*
		 * 上面步骤为处理消息,即解析返回的SOAP消息
		 * 打印结果为:55
		 */
	} catch (MalformedURLException e) {
		e.printStackTrace();
	} catch (SOAPException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
}


 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在C#中发送并处理SOAP格式的XML数据,可以使用System.Net.Http和System.Xml.Linq命名空间中的类和方法。 首先,需要使用System.Net.Http.HttpClient类创建一个HTTP客户端对象,然后使用HttpClient.PostAsync方法发送SOAP消息。在PostAsync方法中,必须指定SOAP消息的内容类型为"text/xml",并将SOAP消息作为字符串传递给HttpContent对象。 以下是一个简单的示例代码: ```csharp using System; using System.Net.Http; using System.Xml.Linq; using System.Text; namespace SoapHttpClient { class Program { static async void SendSoapMessage() { // 构造SOAP消息 XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/"; XNamespace ns = "http://www.example.org/sample"; XElement body = new XElement(ns + "SampleRequest", new XElement(ns + "Parameter", "Value")); XElement envelope = new XElement(soapenv + "Envelope", new XAttribute(XNamespace.Xmlns + "soapenv", soapenv), new XAttribute(XNamespace.Xmlns + "sam", ns), new XElement(soapenv + "Body", body)); string soapMessage = envelope.ToString(); // 创建HTTP客户端对象 HttpClient client = new HttpClient(); // 发送SOAP消息 HttpContent content = new StringContent(soapMessage, Encoding.UTF8, "text/xml"); HttpResponseMessage response = await client.PostAsync("http://example.com/soap-service", content); // 处理响应 if (response.IsSuccessStatusCode) { string responseXml = await response.Content.ReadAsStringAsync(); XDocument responseDoc = XDocument.Parse(responseXml); XElement responseBody = responseDoc.Descendants(ns + "SampleResponse").FirstOrDefault(); if (responseBody != null) { // 处理响应体 } } else { // 处理错误响应 } } static void Main(string[] args) { SendSoapMessage(); Console.ReadKey(); } } } ``` 在这个示例中,我们使用LINQ to XML构造了一个SOAP消息,然后使用HttpClient.PostAsync方法发送SOAP消息。在响应中,我们首先将响应体解析为XML文档,然后使用LINQ to XML查询响应体中的元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值