1

如何在 C# 中使用 ServiceReference 或 WebReference 做到这一点

   class SoapAuthentificationHeader {
        /** @var string sPassword */
        public $Password;
        /** @var string sLogin */
        public $sLogin;
        /** @var string anotherCustomParam*/
        public $sVod;

        public function __construct($sLogin, $sPassword, $sVod) {
            $this->sPassword=$sPassword;
            $this->sLogin=$sLogin;
            $this->sVod=$sVod;
        }
    }

    $oSoap=new SoapClient('http://.[ my path ]...wsdl', array(
        'trace' => 1,
        'encoding' => 'UTF-8'
    ));

    try {
        $oSoap->__setSoapHeaders(array(new SoapHeader('urn:vod_soap', 'AuthenticationHeader', new SoapAuthentificationHeader('login', 'password', 'anotherCustomParam'))));     
$iCountVideo = $oSoap->countVideo();
    } catch (Exception $oException) {
        var_dump($oException);
    }

我试图在我的通话之间实例化一个共享的 CookieContainer。但它没有用:目前我有:

vod_soapService s = new vod_soapService();
            s.CookieContainer = new CookieContainer();

            s.ConnectionGroupName = "test";
             // this webmethod works it return me true 
            s.AuthenticationHeader("foo", "bar", "test");

            string test = s.countVideo();
4

1 回答 1

2

您必须在发送 xml 之前手动插入 soap 标头。要实现这一点,请阅读下面的链接
http://forums.asp.net/t/1137408.aspx

您还必须调整给定的解决方案以适合您的身份验证标头

您的“AfterSerialize”实现应如下所示:

case SoapMessageStage.AfterSerialize:
{
  // Get the SOAP body as a string, so we can manipulate...
  String soapBodyString = getXMLFromCache();

  String BodyString = "<soap:Body";
  int posStartBody = soapBodyString.IndexOf(BodyString);

  // Create the SOAP header Message 
  String soapEnvHeaderString = "<soap:Header><SoapAuthentificationHeader><sLogin>";
  String soapEnvHeaderString2 = "</sLogin><sPassword>";
  String soapEnvHeaderString3 = "</sPassword><sVod>";
  String soapEnvHeaderString4 = "</sVod></SoapAuthentificationHeader></soap:Header>";
  Stream appOutputStream = new MemoryStream();
  StreamWriter soapMessageWriter = new StreamWriter(appOutputStream);
  soapMessageWriter.Write(soapBodyString.Substring(0,posStartBody));
  soapMessageWriter.Write(soapEnvHeaderString);
  soapMessageWriter.Write("your login ");
  soapMessageWriter.Write(soapEnvHeaderString2);
  soapMessageWriter.Write("your password");
  soapMessageWriter.Write(soapEnvHeaderString3);
  soapMessageWriter.Write("your vod");
  soapMessageWriter.Write(soapEnvHeaderString4);
  soapMessageWriter.Write(soapBodyString.Substring(posStartBody));
  // write it all out.
  soapMessageWriter.Flush();
  appOutputStream.Flush();
  appOutputStream.Position = 0;
  StreamReader reader = new StreamReader(appOutputStream);
  StreamWriter writer = new StreamWriter(this.outputStream);
  writer.Write(reader.ReadToEnd());
  writer.Flush();
  appOutputStream.Close();
  this.outgoing = false;
  this.incoming = true;
  break;
}
于 2013-12-23T11:07:53.883 回答