我正在尝试使用ruby库Savon创建SOAP请求。
我使用以下代码:
require "savon"
Savon.configure do |config|
config.soap_version = 2 # use SOAP 1.2
config.raise_errors = false
end
wsdl_logon = Savon::Client.new do
wsdl.document = "https://api.affili.net/V2.0/Logon.svc?wsdl"
end
username = 'XXX'
password = 'YYY'
wsdl_logon.http.headers["Content-Type"] = "text/xml; charset=utf-8"
response = wsdl_logon.request "Logon" do
soap.body = {'Username' => username, 'Password' => password, 'WebServiceType' => 'Product'}
end
if response.http_error?
puts "Http Error!"
puts y response.http_error
else
puts "No Http Error!"
end
但我一直收到400条错误消息(“错误请求”)。或者,如果我删除以下行
wsdl_logon.http.headers["Content-Type"] = "text/xml; charset=utf-8"
我收到415条错误消息(“不支持的媒体类型”)。
到目前为止,我一直在使用PHP来发出这些请求,以下代码始终没有问题:
$soap_logon = new SoapClient('https://api.affili.net/V2.0/Logon.svc?wsdl');
$token = $soap_logon->Logon(array(
'Username' => 'XXX',
'Password' => 'YYY',
'WebServiceType' => 'Product'
));
任何人都可以指出正确的方向可能是错误来源吗?我现在完全失去了。
感谢您的帮助。
我按照Tom De Leu的建议做了,并尝试尽可能多地删除生成的SOAP请求中的差异。但我仍然收到400错误。任何暗示可能的原因都将受到高度赞赏。
这是由PHP生成的(工作)请求(为了清楚起见,添加了XML中的换行符):
POST /V2.0/Logon.svc HTTP/1.1
Host: api.affili.net
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.2.0-8+etch16
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://affilinet.framework.webservices/Svc/ServiceContract1/Logon"
Content-Length: 456
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://affilinet.framework.webservices/types"
xmlns:ns2="http://affilinet.framework.webservices/Svc"
>
<SOAP-ENV:Body>
<ns2:LogonRequestMsg>
<ns1:Username>xxx</ns1:Username>
<ns1:Password>yyy</ns1:Password>
<ns1:WebServiceType>Product</ns1:WebServiceType>
</ns2:LogonRequestMsg>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
这是Ruby生成的(不工作)请求(为了清晰起见,再次添加了xml换行符)
SOAP request: https://api.affili.net/V2.0/Logon.svc
Content-Type: text/xml; charset=utf-8, SOAPAction: http://affilinet.framework.webservices/Svc/ServiceContract1/Logon, Content-Length: 605
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:wsdl="http://affilinet.framework.webservices/Svc"
SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://affilinet.framework.webservices/types"
xmlns:ns2="http://affilinet.framework.webservices/Svc"
>
<SOAP-ENV:Body>
<ns2:LogonRequestMsg>
<ns1:Username>XXX</ns1:Username>
<ns1:Password>YYY</ns1:Password>
<wsdl:WebServiceType>Product</wsdl:WebServiceType>
</ns2:LogonRequestMsg>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
HTTPI executes HTTP POST using the httpclient adapter
SOAP response (status 400):
答案 0 :(得分:3)
我发现我需要添加标题以超越415错误。
Savon.client(wsdl:“www.sample_doman.com/endpoint?wsdl”,标题:{'Content-Type'=&gt;'application / soap + xml; charset = utf-8'})
答案 1 :(得分:2)
我建议查看PHP代码发送的XML,然后将其与Ruby Savon代码发送的XML进行比较,并检查差异的位置。然后看看是否可以修改ruby代码以生成正确的请求。
答案 2 :(得分:0)
告诉Savon使用SOAP版本2(真正的1.2),然后手动将内容类型设置为text / xml类型,这样就失败了。
如果您的Web服务需要SOAP 1.2,那么期望内容类型为&#39; application / soap + xml&#39;,如果您将soap_version设置为2,SAVON将为您执行此操作。
如果您想要text / xml的内容类型,只需将soap_version配置变量设置为1
即可