我正在尝试将带有身份验证凭据的soap 标头从PHP 发送到ASP.NET Web 服务。
/// <summary>
/// Soap Header for the Secured Web Service.
/// </summary>
public class TestWebServiceHeader : System.Web.Services.Protocols.SoapHeader
{
public string Username;
public string Password;
public string AuthenticatedToken;
}
/// <summary>
/// Summary description for TestService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class TestService : System.Web.Services.WebService
{
public TestWebServiceHeader AuthHeader;
public SoapUnknownHeader[] UnknownHeaders;
[WebMethod]
[System.Web.Services.Protocols.SoapHeader("AuthHeader")]
[SoapHeader("UnknownHeaders")]
public string HelloWorld()
{
foreach (SoapUnknownHeader header in UnknownHeaders)
{
if (header.Element.LocalName == "AuthHeader")
{
// FOUND HEADER
}
}
if (AuthHeader == null)
{
// NULL AuthHeader
}
return "Hello World";
}
}
这是PHP
<?php
$service = new SoapClient("http://localhost/Orientation/TestService.asmx?wsdl");
$HeaderSecurity = array("Username"=>"user", "Password"=>"pass123");
$header = new SoapHeader('http://tempuri.org/', 'AuthHeader', $HeaderSecurity, false);
$service->__setSoapHeaders(array($header));
$service->HelloWorld(); // Example
//$service = new SoapClient("http://localhost/Orientation/TestService.asmx?wsdl");
//$auth = array('user'=>'user', 'password'=>'pass123');
//$authvar = new SoapVar($auth, SOAP_ENC_OBJECT);
//$header = new SoapHeader('http://tempuri.org/', 'AuthHeader xmlns="http://tempuri.org/"', $authvar, false);
//$service->__setSoapHeaders(array($header));
//$service->HelloWorld();
?>
在 Web 服务中,来自 PHP 的 AuthHeader 始终为空。我可以在捕获 SoapUnknownHeaders 时找到标头,并且标头名称为 ns1:AuthHeader。如何在不使用 SoapUnknownHeaders 的情况下获取标题。拥有标题对象后,如何从标题中提取信息?我也无法让注释掉的部分在 PHP 中工作。SOAP_ENC_OBJECT 是否使调用更安全?