gpt4 book ai didi

java - SOAP 客户端,以下示例

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:19:11 26 4
gpt4 key购买 nike

我需要为我的公司使用名为“Mouser”的 SOAP 服务器。但是,当我尝试发送消息时遇到问题。

我的请求的文档是:

POST /service/searchapi.asmx HTTP/1.1
Host: www.mouser.fr
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Header>
<MouserHeader xmlns="http://api.mouser.com/service">
<AccountInfo>
<PartnerID>string</PartnerID>
</AccountInfo>
</MouserHeader>
</soap12:Header>
<soap12:Body>
<SearchByPartNumber xmlns="http://api.mouser.com/service">
<mouserPartNumber>string</mouserPartNumber>
</SearchByPartNumber>
</soap12:Body>
</soap12:Envelope>

好的,现在我将向您介绍如何使用我发送的消息发送我的 Java 代码:

String mpns = "BAV99";

SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();

SOAPMessage message = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();

MimeHeaders mimeHeader = message.getMimeHeaders();
mimeHeader.setHeader("Content-Type", "application/soap+xml; charset=utf-8");

SOAPPart soapPart = message.getSOAPPart();
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
+ "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n"
+ " <soap12:Header>\n"
+ " <MouserHeader xmlns=\"http://api.mouser.com/service\">\n"
+ " <AccountInfo>\n"
+ " <PartnerID>" + key + "</PartnerID>\n"
+ " </AccountInfo>\n"
+ " </MouserHeader>\n"
+ " </soap12:Header>\n"
+ " <soap12:Body>\n"
+ " <SearchByPartNumber xmlns=\"http://api.mouser.com/service\">\n"
+ " <mouserPartNumber>" + mpns + "</mouserPartNumber>\n"
+ " </SearchByPartNumber>\n"
+ " </soap12:Body>\n"
+ "</soap12:Envelope>";

StreamSource source = new StreamSource(new StringReader(xml));
soapPart.setContent(source);
message.saveChanges();

System.out.println("Send : ");
message.writeTo(System.out);
System.out.println();

java.net.URL endpoint = new URL(targetUrl);
SOAPMessage reply = connection.call(message, endpoint);

StringWriter sw = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(message.getSOAPPart()), new StreamResult(sw));
connection.close();
System.out.println("Received : ");
System.out.println(sw.toString());
return sw.toString();

我没有从服务器得到我想要的响应,而是得到了我发送的相同消息但是带有新的属性:standalone = "no"这是什么意思?为什么这个 react ?

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Header>
<MouserHeader xmlns="http://api.mouser.com/service">
<AccountInfo>
<PartnerID>key</PartnerID>
</AccountInfo>
</MouserHeader>
</soap12:Header>
<soap12:Body>
<SearchByPartNumber xmlns="http://api.mouser.com/service">
<mouserPartNumber>BAV99</mouserPartNumber>
</SearchByPartNumber>
</soap12:Body>
</soap12:Envelope>

感谢您的帮助!

最佳答案

实际上,您可以使用 soap ui 生成类。您的程序可以使用创建的类轻松调用服务,而无需构建您自己的请求 header 和正文但是你需要一些图书馆。示例java jdk自带jax-ws lib

教程:http://www.soapui.org/soap-and-wsdl/soap-code-generation.html

关于java - SOAP 客户端,以下示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30976157/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com