gpt4 book ai didi

java - Twinfield - 服务器无法识别 HTTP header SOAPAction 的值

转载 作者:行者123 更新时间:2023-11-30 08:59:51 25 4
gpt4 key购买 nike

我正在尝试连接 Twinfield logon api通过Java。我试过的代码是

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 上运行良好.有人可以指出我的程序哪里出错了吗?

最佳答案

因为故障代码是客户端,所以是您发送的 soap 消息导致了问题。尝试使用 SOAP UI 传递相同的内容,我尝试将 WSDL 导入到我的 SOAP UI 中,但出现附加错误...

加载错误 [ https://login.twinfield.com/webservices/session.asmx?wsdl] : org.apache.xmlbeans.XmlException: org.apache.xmlbeans.XmlException: 错误: 不关闭标签

由于显示的 Web 服务是文档/文字包装的,只需尝试使用 wsimport 创建 hte 客户端代码,您将拥有所有需要的文件,并且可以轻松发送相同的请求。

如果您只对 SAAJ 方式感兴趣,那么我会尝试传递通过 SOAP UI 创建的消息。

希望这对您有所帮助。

关于java - Twinfield - 服务器无法识别 HTTP header SOAPAction 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27069276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com