2015-06-22 60 views
3

我需要爲我的公司使用名爲「Mouser」的SOAP服務器。但是,當我嘗試發送消息時,我遇到了問題。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 =「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> 

感謝您的幫助!

+0

你的WSDL文件是什麼樣的?您能否將相關部分的服務WSDL發佈到這個問題上?你有沒有考慮過使用Apache CXF? –

+0

嘗試使用SOAPUI發送請求,並看看你得到了什麼。 – Garry

+0

這是WSDL文件的鏈接:http://www.mouser.fr/service/searchapi.asmx?WSDL 您認爲這是SAAJ的問題嗎? Apache CXF是相當的? –

回答

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);