2014-11-21 60 views
2

我試圖通過Java連接Twinfield logon api。我試過的代碼是Twinfield - 服務器無法識別HTTP標頭的值SOAPAction

import java.net.URL; 
import javax.xml.namespace.QName; 
import javax.xml.soap.MessageFactory; 
import javax.xml.soap.SOAPBody; 
import javax.xml.soap.SOAPBodyElement; 
import javax.xml.soap.SOAPConnection; 
import javax.xml.soap.SOAPConnectionFactory; 
import javax.xml.soap.SOAPElement; 
import javax.xml.soap.SOAPHeader; 
import javax.xml.soap.SOAPMessage; 
import javax.xml.soap.MimeHeaders; 
import java.io.ByteArrayInputStream; 
import javax.xml.transform.*; 
import javax.xml.transform.stream.*; 

public class SoapTest { 

    public static void main(String[] args) { 
     try { 
      SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance(); 
      SOAPConnection connection = sfc.createConnection(); 

      MessageFactory mf = MessageFactory.newInstance(); 
      SOAPMessage sm = mf.createMessage(); 

      SOAPHeader sh = sm.getSOAPHeader(); 
      SOAPBody sb = sm.getSOAPBody(); 
      //sh.detachNode(); 
      QName logonName = new QName("http://www.twinfield.com", "Logon"); 
      SOAPBodyElement logonElement = sb.addBodyElement(logonName); 

      QName userTag = new QName("user"); 
      SOAPElement user = logonElement.addChildElement(userTag); 
      user.addTextNode("myuser"); 

      QName passwordTag = new QName("password"); 
      SOAPElement password = logonElement.addChildElement(passwordTag); 
      password.addTextNode("mypassword"); 

      QName organisationTag = new QName("organisation"); 
      SOAPElement organisation = logonElement.addChildElement(organisationTag); 
      organisation.addTextNode("myorg"); 

      System.out.println("\n Soap Request:\n"); 
      sm.writeTo(System.out); 
      System.out.println(); 

      URL endpoint = new URL("https://login.twinfield.com/webservices/session.asmx"); 
      SOAPMessage response = connection.call(sm, endpoint); 

      connection.close(); 

      //System.out.println(response.getContentDescription()); 
      //System.out.println("--------------------------"); 

      // Reading response 
      printSOAPResponse(response); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception { 
     TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
     Transformer transformer = transformerFactory.newTransformer(); 
     Source sourceContent = soapResponse.getSOAPPart().getContent(); 
     System.out.print("\nResponse SOAP Message = "); 
     StreamResult result = new StreamResult(System.out); 
     transformer.transform(sourceContent, result); 
    } 
} 

,似乎一切都是正確的,但我經常得到響應<faultstring>Server did not recognize the value of HTTP Header SOAPAction: .</faultstring>。上述程序的完整輸出如下:

java SoapTest 

Soap Request: 

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
<SOAP-ENV:Header/> 
    <SOAP-ENV:Body> 
     <Logon xmlns="http://www.twinfield.com"> 
      <user>NLG001136</user> 
      <password>qura976yj</password> 
      <organisation>TWF-SAAS</organisation> 
     </Logon> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

Response SOAP Message = 
<?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
     <soap:Fault> 
      <faultcode>soap:Client</faultcode> 
      <faultstring>Server did not recognize the value of HTTP Header SOAPAction: .</faultstring> 
      <detail/> 
     </soap:Fault> 
    </soap:Body> 
</soap:Envelope> 

相同的請求/憑據上Soapclient.com工作的罰款。有人可以指出我的程序出錯了嗎?

回答

2

由於故障代碼是客戶端,它是您發送的肥皂消息導致的問題。嘗試傳遞相同的使用SOAP UI,我試圖相同的將WSDL導入到我的SOAP用戶界面,但我得到附加錯誤...

加載時出錯[https://login.twinfield.com/webservices/session.asmx?wsdl]:org.apache.xmlbeans.XmlException:org.apache。 xmlbeans.XmlException:錯誤:不關閉標記

由於webservice的展示是Document/Literal包裝,只需嘗試使用wsimport創建hte客戶端代碼,並且您將擁有所有需要的文件並且很容易發送相同的請求。

如果您只是對SAAJ方式感興趣,那麼我會嘗試傳遞通過SOAP UI創建的消息。

希望這會有所幫助。

+0

將賞金視爲答案。 雖然我的問題現在已經解決,並且答案給了我方向,但它不是一個完整的答案,所以不接受答案。 – 2014-12-01 10:44:26

相關問題