gpt4 book ai didi

java - SOAP 请求在 Java 上不起作用,但在 PHP 上起作用

转载 作者:行者123 更新时间:2023-12-02 08:40:00 24 4
gpt4 key购买 nike

我一直在尝试查找有关此问题的信息,但似乎找不到与我的问题完全相关的任何信息。

当我使用 PHP 类 SoapClient 进行 SOAP 调用时,请求完美运行。但是当我尝试在 Java 上做同样的事情时,我得到一个异常,说 URL 不接受 POST,我见过人们使用 Curl 在 PHP 上使用 POST 构建 Soap 调用,但我真的不知道 SoapClient 是什么做。

无论如何,这是 PHP 代码:

$url    = 'https://mail.myzimbra.mx/service/wsdl/ZimbraAdminService.wsdl';
$soap_client = new SoapClient($url, array("soap_version" => SOAP_1_1,"trace" => 1));

$ns = 'urn:zimbra';

$headerbody = array('context' => '');

//Create Soap Header.
$header = new SOAPHeader($ns, 'RequestorCredentials', $headerbody);

//set the Headers of Soap Client.
$soap_client->__setSoapHeaders($header);

$AuthRequestObjectXML = '<AuthRequest xmlns="urn:zimbraAdmin" password="password">
<account by="adminName">user@zimbra.mx</account>
</AuthRequest>';


$AuthRequestObject = new SoapVar($AuthRequestObjectXML,XSD_ANYXML);

$objResponse = $soap_client->__soapCall(
'AuthRequest',
array($AuthRequestObject)
);

$lastrequest = $soap_client->__getLastRequest();


$token = $objResponse->authToken;

它并不花哨,但它可以完成工作。

现在,在java上(我已经做了几次尝试,但这是我最后一次尝试):

String send = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">" +
"<soap:Header xmlns=\"urn:zimbra\">" +
"<context></context>" +
"</soap:Header>" +
"<soap:Body>" +
"<AuthRequest xmlns=\"urn:zimbraAdmin\" password=\""+ ZIMBRA_ADMIN_PASSWORD +"\">" +
"<account by=\"adminName\">"+ ZIMBRA_ADMIN_ACCOUNT +"</account>" +
"</AuthRequest>" +
"</soap:Body>" +
"</soap:Envelope>";

SOAPConnectionFactory sfc = SOAPConnectionFactory.newInstance();
SOAPConnection connection = sfc.createConnection();
InputStream is = new ByteArrayInputStream(send.getBytes());
SOAPMessage request = MessageFactory.newInstance().createMessage(null, is);

request.writeTo(System.out); //Here it prints my envelop, which is formed just like the PHP one.

URL endpoint = new URL(URL_ZIMBRA_ADMIN_WSDL);
SOAPMessage response = connection.call(request, endpoint);

变量确实与 PHP 值匹配,它们完全相同,我已经验证了一百次。

但马蹄莲需要几秒钟,我猜大约 20 秒,然后我就明白了

com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (400HTTP method POST is not supported by this URL
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:149)
at com.project.services.ZimbraService.getToken(ZimbraService.java:79)

*more stuff*

CAUSE:

com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (400HTTP method POST is not supported by this URL
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:264)
at com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:145)

我已经尝试了几件事,例如将协议(protocol)更改为 SOAPConstants.SOAP_1_2_PROTOCOL,因此请求 header 内容类型更改为 application/soap+xml,但似乎没有任何效果,我总是收到 URL 不允许的 POST 异常。还尝试了我读到的有关更改 URL 以删除 wsdl 文件部分的其他一些线程,但没有运气。

那么,如果它可以在 PHP 上运行,为什么它不能在 Java 上运行呢?我在同一台机器上运行这两个脚本。

最佳答案

PHP 的 SoapClient具有所谓的“WSDL 模式”,当您为其提供 WSDL 的 URL 时,它会下载该 WSDL,并从 WSDL 中提取真实端点 URL

Java 的 SOAPConnection没有“WSDL 模式”,因此您需要向 call() 方法提供真正的端点 URL,而不是 WSDL URL。

如果您不知道真正的端点 URL,请执行 SoapClient 的操作,自行下载 WSDL 并查看。结束点 URL 将位于末尾。

来自example WSDL :

  ...

<service name="EndorsementSearchService">
<documentation>snowboarding-info.com Endorsement Service</documentation>
<port name="GetEndorsingBoarderPort"
binding="es:EndorsementSearchSoapBinding">
<soap:address location="http://www.snowboard-info.com/EndorsementSearch"/>
</port>
</service>

</definitions>

这里真正的终点URL是:http://www.snowboard-info.com/EndorsementSearch

关于java - SOAP 请求在 Java 上不起作用,但在 PHP 上起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61435573/

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