SOAP客户端,跟随一个例子

3
我需要为我的公司使用名为"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"。这是什么意思?为什么会出现这种响应?

<?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>

谢谢帮助!


你的 WSDL 文件是什么样子的?你能在这个问题中发布服务 WSDL 的相关部分吗?你考虑使用 Apache CXF 吗? - Tim Biegeleisen
尝试使用SOAPUI发送请求并查看结果。 - Garry
这是 WSDL 文件的链接:http://www.mouser.fr/service/searchapi.asmx?WSDL你认为这是 SAAJ 的问题吗?Apache CXF 是一个等效的替代方案吗? - François Dupont
我试图使用SOAPUI,但我认为需要一个有效的密钥。这是我得到的信息:“服务器无法处理请求。---> 无效的唯一标识符”。 - Garry
我尝试使用与SOAP-UI相同的消息和相同的目标,但我总是从服务器获得相同的响应。 - François Dupont
显示剩余2条评论
2个回答

2

0

我找到了解决方法!感谢大家,特别是foolvoe99,因为是你的想法让我知道了该去哪里搜索。

我使用了"wsimport"从WSDL生成Java类并使用它们。以下是我的做法,希望能帮助其他人:

URL wsdlLocation = new URL("your_wsdl_target");
QName apiName = new QName("your_service_target", "your_service_name");
your_service_name api = new your_service_name(wsdlLocation, apiName);
api.addPort(your_service_name, SOAPBinding.SOAP12HTTP_BINDING, "your_service_target/name");

QName port_name = new QName("your_service_target", "port_name");
Dispatch<SOAPMessage> disp = api.createDispatch(port_name, SOAPMessage.class, Service.Mode.MESSAGE);            

String xml = "Your SOAP MESSAGE";

MessageFactory mf = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage request = mf.createMessage();
SOAPPart part = request.getSOAPPart();
StreamSource source = new StreamSource(new StringReader(xml));
part.setContent(source);
request.saveChanges();

SOAPMessage response = disp.invoke(request);

StringWriter sw = new StringWriter();
TransformerFactory.newInstance().newTransformer().transform(new DOMSource(response.getSOAPPart()), new StreamResult(sw));
org.json.JSONObject xmlJSONObj = XML.toJSONObject(sw.toString());
return xmlJSONObj.toString(2);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接