2014-11-21 67 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

相关问题