【发布时间】:2020-08-09 15:10:30
【问题描述】:
我一直在尝试查找有关此的信息,但似乎找不到与我的问题完全相关的任何信息。
当我使用 PHP 类 SoapClient 进行肥皂调用时,请求可以完美运行。但是当我尝试在 Java 上做同样的事情时,我得到一个异常,说 URL 不接受 POST,我看到人们使用 Curl 在 PHP 上构建 Soap 调用,使用 POST,但我真的不知道 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)
我尝试了几件事,比如将协议更改为 SOAPConstants.SOAP_1_2_PROTOCOL,因此请求标头内容类型更改为 application/soap+xml,但似乎没有任何效果,我总是得到 URL 异常不允许的 POST。还尝试了一些我读过的关于更改 URL 以删除 wsdl 文件部分的其他线程,但没有运气。
那么,如果它可以在 PHP 上运行,为什么它不能在 java 上运行呢?我在同一台机器上运行这两个脚本。
【问题讨论】:
标签: java php xml web-services soap