【问题标题】:SoapFault exception: [HTTP] Unsupported Media Type when accessing Java web-service from PHPSoapFault 异常:[HTTP] 从 PHP 访问 Java Web 服务时不受支持的媒体类型
【发布时间】:2009-10-24 12:17:09
【问题描述】:

我正在尝试使用 Zend Framework v1.9.0 中的 Zend_Soap_Client 连接到 Java Web 服务:

<?php
include( 'Zend/Loader/Autoloader.php');
$autoloader = Zend_Loader_Autoloader::getInstance();
$client = new Zend_Soap_Client('https://webservice.com/webservice-war/webservice?wsdl'
    , array('encoding'=> 'UTF-8'));

try{
    $result = $client->find_customer(array('username' => 'user', 
                         'password' => '123'), array('city' => 'some city'));
} catch(Exception $e){
    echo $e;
}

echo '<pre>' . $client->getLastRequestHeaders() . '</pre>'; 
?>

输出:

SoapFault exception: [HTTP] Unsupported Media Type in 
/Library/ZendFramework-1.9.0/library/Zend/Soap/Client.php:937 
Stack trace: 
 #0 [internal function]:
SoapClient->__doRequest('_doRequest(Object(Zend_Soap_Client_Common),
    '__doRequest('__soapCall('find_customer', Array, NULL, NULL, Array) 
 #6 [internal function]:  
 Zend_Soap_Client->__call('find_customer', Array) 
 #7 /Users/webservicetest/index.php(8): 
 Zend_Soap_Client->find_customer(Array, Array) 
 #8 {main}

POST /webservice-war/webservice HTTP/1.1
Host: webservice.com
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.2.6
Content-Type: application/soap+xml; charset=utf-8; action=""
Content-Length: 315

知道有什么问题吗?网址是正确的,因为我在调用时得到了可用的功能

$client->getFunctions()

【问题讨论】:

    标签: java php zend-framework soap web-services


    【解决方案1】:

    根据this listing,异常表明托管网络服务的服务器对您的请求编码不满意:

    表示对端HTTP服务器 不支持使用的 Content-type 对请求消息进行编码。这 消息交换被视为具有 未成功完成。

    因此,您应该向网络服务提供商咨询他们期望的内容类型/编码。

    如果您使用SOAP_1_2,一个可能的解决方案是更改为SOAP_1_1,因为这会改变发出的请求。

    【讨论】:

    • 更具体:framework.zend.com/issues/browse/ZF-5286。通过在选项中设置 'soap_version' => SOAP_1_1 解决,使请求通过 text/xml 而不是 application/soap+xml
    • @MadsMobæk Brilliant,解决了我遇到的同样问题。
    • 尝试添加header Content-Type: text/xml; charset=utf-8 for soap 1.1 和 Content-Type: application/soap+xml; charset=utf-8 用于肥皂 1.2
    【解决方案2】:

    我没有使用 Zend 框架,但在 JavaScript 中使用 XMLHttpRequest 时遇到了类似的问题。解决方案是在 SOAP 请求标头中指定 Content-Type。

    var sr = '<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.w3schools.com/webservices/">  <SOAP-ENV:Body><ns1:CelsiusToFahrenheit><ns1:Celsius>32</ns1:Celsius></ns1:CelsiusToFahrenheit></SOAP-ENV:Body></SOAP-ENV:Envelope>';
    http_request = new XMLHttpRequest();
    http_request.open('POST', 'http://www.w3schools.com/webservices/tempconvert.asmx', true);
    http_request.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    http_request.send(sr);
    

    【讨论】:

      猜你喜欢
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多