SOAP客户端,举个例子

时间:2015-06-22 09:15:05

标签: java soap saaj

我需要使用名为" 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();

我没有从服务器获得我想要的响应,而是收到了我发送但是使用新的ATTRIBUTE的相同消息:standalone =&#34; no&#34; 这是什么意思?为什么这个回应?

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

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

我找到了怎么做!非常感谢大家,特别是 foolvoe99 ,因为您的想法是我知道在哪里搜索。

我用过&#34; wsimport&#34;从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);