使用 java 向 WebService 发出 SOAP 请求

编程入门 行业动态 更新时间:2024-10-26 19:37:21
本文介绍了使用 java 向 WebService 发出 SOAP 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对如何通过 java 向网络服务发出请求有点困惑.

I'm a bit confused about how to make a request to a webservice via java.

目前我唯一了解的是 webservices 使用 xml 结构化消息,但我仍然不太明白如何构建我的请求.

For now the only thing that I've understand is that webservices uses xml structured messages, but still I didn't quite understood how to structure my request.

<soap:Envelope xmlns:soap="schemas.xmlsoap/soap/envelope/"> <soap:Body> <getProductDetails xmlns="magazzino.example/ws"> <productId>827635</productId> </getProductDetails> </soap:Body> </soap:Envelope>

基本上,我必须向 Web 服务发送 2 个参数,作为回报,我需要另外两个参数.

Basically I've to send 2 parameters to the web service and in return I expect two other parameters.

我想有一些罐子可以完成大部分工作,但我没有在网上找到任何罐子.有人可以解释我的依据吗?

I guess there are some jars that can do most of the job, but I didn't find any online. Can someone please explain me the basis?

推荐答案

SOAP 请求是一个 XML 文件,由您发送到服务器的参数组成.

A SOAP request is an XML file consisting of the parameters you are sending to the server.

SOAP 响应同样是一个 XML 文件,但现在包含服务想要为您提供的所有内容.

The SOAP response is equally an XML file, but now with everything the service wants to give you.

基本上,WSDL 是一个 XML 文件,用于解释这两个 XML 的结构.

Basically the WSDL is a XML file that explains the structure of those two XML.

要在 Java 中实现简单的 SOAP 客户端,您可以使用 SAAJ 框架(它随 JSE 1.6 及更高版本提供):

To implement simple SOAP clients in Java, you can use the SAAJ framework (it is shipped with JSE 1.6 and above):

SOAP with Attachments API for Java (SAAJ) 主要用于直接处理发生在任何 Web 服务 API 的幕后的 SOAP 请求/响应消息.它允许开发人员直接发送和接收soap消息,而不是使用JAX-WS.

SOAP with Attachments API for Java (SAAJ) is mainly used for dealing directly with SOAP Request/Response messages which happens behind the scenes in any Web Service API. It allows the developers to directly send and receive soap messages instead of using JAX-WS.

请参阅下面的使用 SAAJ 的 SOAP Web 服务调用的工作示例(运行它!).它调用这个网络服务.

See below a working example (run it!) of a SOAP web service call using SAAJ. It calls this web service.

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: www.webservicex/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 = "www.webservicex/uszip.asmx"; String soapAction = "www.webserviceX.NET/GetInfoByCity"; callSoapWebService(soapEndpointUrl, soapAction); } private static void createSoapEnvelope(SOAPMessage soapMessage) throws SOAPException { SOAPPart soapPart = soapMessage.getSOAPPart(); String myNamespace = "myNamespace"; String myNamespaceURI = "www.webserviceX.NET"; // SOAP Envelope SOAPEnvelope envelope = soapPart.getEnvelope(); envelope.addNamespaceDeclaration(myNamespace, myNamespaceURI); /* Constructed SOAP Request Message: <SOAP-ENV:Envelope xmlns:SOAP-ENV="schemas.xmlsoap/soap/envelope/" xmlns:myNamespace="www.webserviceX.NET"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <myNamespace:GetInfoByCity> <myNamespace:USCity>New York</myNamespace:USCity> </myNamespace:GetInfoByCity> </SOAP-ENV:Body> </SOAP-ENV:Envelope> */ // 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(" Error occurred while sending SOAP Request to Server! Make sure you have the correct endpoint URL and SOAPAction! "); 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(" "); return soapMessage; } }

更多推荐

使用 java 向 WebService 发出 SOAP 请求

本文发布于:2023-10-09 18:48:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476487.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:java   WebService   SOAP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!