java webservice soap请求_使用Java对WebService的SOAP请求

本文展示了如何使用Java的SAAJ库创建一个SOAP客户端,调用WebService并处理SOAP请求和响应。示例代码详细解释了如何设置SOAP端点、操作、构建SOAP消息以及发送和接收XML响应。
摘要由CSDN通过智能技术生成

小编典典

SOAP请求是一个XML文件,包含要发送到服务器的参数。

SOAP响应同样是一个XML文件,但是现在该服务希望提供给您一切。

基本上,WSDL是一个XML文件,解释了这两个XML的结构。

要使用Java实现简单的SOAP客户端,可以使用SAAJ框架(JSE 1.6及更高版本附带):

带有Java附件API的SOAP(SAAJ) 主要用于直接处理任何Web Service

API幕后发生的SOAP请求/响应消息。它允许开发人员直接发送和接收肥皂消息,而不是使用JAX-WS。

参见下面使用SAAJ进行SOAP

Web服务调用的工作示例(运行它!)。它称为此Web服务。

import javax.xml.soap.*;

public class SOAPClientSAAJ {

// SAAJ - SOAP Client Testing

public static void main(String args[]) {

/*

The example below requests from the Web Service at:

http://www.webservicex.net/uszip.asmx?op=GetInfoByCity

To call other WS, change the parameters below, which are:

- the SOAP Endpoint URL (that is, where the service is responding from)

- the SOAP Action

Also change the contents of the method createSoapEnvelope() in this class. It constructs

the inner part of the SOAP envelope that is actually sent.

*/

String soapEndpointUrl = "http://www.webservicex.net/uszip.asmx";

String soapAction = "http://www.webserviceX.NET/GetInfoByCity";

callSoapWebService(soapEndpointUrl, soapAction);

}

private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException {

SOAPPart soapPart = soapMessage.getSOAPPart();

String myNamespace = "myNamespace";

String myNamespaceURI = "http://www.webserviceX.NET";

// SOAP Envelope

SOAPEnvelope envelope = soapPart.getEnvelope();

envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI);

/*

Constructed SOAP Request Message:

New York

*/

// SOAP Body

SOAPBody soapBody = envelope.getBody();

SOAPElement soapBodyElem = soapBody.addChildElement("GetInfoByCity", myNamespace);

SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("USCity", myNamespace);

soapBodyElem1.addTextNode("New York");

}

private static void callSoapWebService(String soapEndpointUrl, String soapAction) {

try {

// Create SOAP Connection

SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();

SOAPConnection soapConnection = soapConnectionFactory.createConnection();

// Send SOAP Message to SOAP Server

SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(soapAction), soapEndpointUrl);

// Print the SOAP Response

System.out.println("Response SOAP Message:");

soapResponse.writeTo(System.out);

System.out.println();

soapConnection.close();

} catch (Exception e) {

System.err.println("\nError occurred while sending SOAP Request to Server!\nMake sure you have the correct endpoint URL and SOAPAction!\n");

e.printStackTrace();

}

}

private static SOAPMessage createSOAPRequest(String soapAction) throws Exception {

MessageFactory messageFactory = MessageFactory.newInstance();

SOAPMessage soapMessage = messageFactory.createMessage();

createSoapEnvelope(soapMessage);

MimeHeaders headers = soapMessage.getMimeHeaders();

headers.addHeader("SOAPAction", soapAction);

soapMessage.saveChanges();

/* Print the request message, just for debugging purposes */

System.out.println("Request SOAP Message:");

soapMessage.writeTo(System.out);

System.out.println("\n");

return soapMessage;

}

}

2020-09-08

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值