Java 上的 SOAP 请求不起作用,但在 PHP 上起作用 [英] SOAP request on Java not working, but does on PHP

查看:38
本文介绍了Java 上的 SOAP 请求不起作用,但在 PHP 上起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

当我使用 PHP 类 SoapClient 进行soap 调用时,请求工作正常.但是当我尝试在 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 = '瓮:zimbra';$headerbody = array('context' => '');//创建肥皂头.$header = new SOAPHeader($ns, 'RequestorCredentials', $headerbody);//设置 Soap 客户端的标题.$soap_client->__setSoapHeaders($header);$AuthRequestObjectXML = '<account by="adminName">user@zimbra.mx</account></AuthRequest>';$AuthRequestObject = new SoapVar($AuthRequestObjectXML,XSD_ANYXML);$objResponse = $soap_client->__soapCall('验证请求',数组($AuthRequestObject));$lastrequest = $soap_client->__getLastRequest();$token = $objResponse->authToken;

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

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

String send = ""+"<soap:Header xmlns=\"urn:zimbra\">"+<上下文></上下文>"+"</soap:Header>"+<肥皂:身体>"+"<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 连接 = sfc.createConnection();InputStream is = new ByteArrayInputStream(send.getBytes());SOAPMessage request = MessageFactory.newInstance().createMessage(null, is);request.writeTo(System.out);//这里打印我的信封,它的形成就像 PHP 的信封一样.URL 端点 = 新 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在 com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:149)在 com.project.services.ZimbraService.getToken(ZimbraService.java:79)*更多东西*原因:com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (400HTTP method POST is not supported by this URL在 com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:264)在 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?我在同一台机器上运行这两个脚本.

解决方案

PHP 的 SoapClient 具有所谓的WSDL 模式",当您为其提供 WSDL 的 URL 时,它会下载该 WSDL,并提取真正的端点 URL 来自 WSDL.

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

如果您不知道真正的端点 URL,请执行 SoapClient 所做的,自己下载 WSDL 并查看它.终点网址将在末尾.

来自示例WSDL:

<块引用>

 ...<服务名称="EndorsementSearchService"><documentation>snowboarding-info.com 背书服务</documentation><端口名称="GetEndorsingBoarderPort"binding="es:EndorsementSearchSoapBinding"><soap:address location="http://www.snowboard-info.com/EndorsementSearch"/></端口></服务></定义>

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

I've been trying to find information about this, but I can't seem to find anything related exactly to my problem.

When I make a soap call, using PHP class SoapClient, the request works perfectly. But when I try to do the same on Java, I get an exception, saying the URL doesn't accept POST, I've seen people building Soap calls on PHP with Curl, using POST, but I don't really know what SoapClient do.

Anyway, this is the PHP Code:

$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;

It isn't fancy, but it gets the job done.

Now, on java (I've done several attempts, but this is the last I tried) :

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);

Variables do match PHP values, they are exactly the same, I've verified this a hundred times.

But the calla takes some seconds, about 20, I guess, then I get

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)

I've tried several things, like changing protocol to SOAPConstants.SOAP_1_2_PROTOCOL, so request header content type is change to application/soap+xml, but nothing seems to work, I always get the POST not allowed by URL exception. Also tried some other thread I read about changing the URL to remove the wsdl file part, but no luck.

So, if it works on PHP, why doesn't it work on java? I'm running both scripts on the same machine.

解决方案

PHP's SoapClient has what is called "WSDL mode", where when you give it the URL for the WSDL, it downloads that WSDL, and extracts the real end point URL from the WSDL.

Java's SOAPConnection does not have a "WSDL mode", so you need to provide the real end point URL to the call() method, not the WSDL URL.

If you don't know the real end point URL, do what SoapClient does, download the WSDL yourself and look at it. The end point URL will be at the end.

From 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>

Here the real end point URL is: http://www.snowboard-info.com/EndorsementSearch

这篇关于Java 上的 SOAP 请求不起作用,但在 PHP 上起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆