douhuigan8063
2014-09-25 14:16
浏览 105

C#中的SoapHeader身份验证(cleverelements)

I am currently trying to consume a SOAP WebService via C#.

The WebServices uses some header authentication I don't get working in C# There is some sample PHP Code, but since it's my first SOAP Client I don't really get the C# equivalent.

I hope somebody can tell me how to do the following in C#:

$client = new SoapClient("http://api.sendcockpit.com/server.php?wsdl"); 
class SOAPAuth{     
    public $userid;     
    public $apikey;
    public $version;
    public $mode;
    public function __construct($userid, $apikey, $version, $mode = 'test') {
        $this--->userid = $userid;
        $this->apikey = $apikey;
        $this->version = $version;
        $this->mode = $mode;
    }
}

$auth = new SOAPAuth('User ID','API Key','1.0','test');

$header = new SOAPHeader('sendcockpit', 'validate', $auth);

$client->__setSoapHeaders($header);

try{
    //get all subscriber lists from account
    $response=$client->apiGetList();
}
catch (SoapFault $exception) {
    echo ($exception->getMessage());
}
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanhuilao0787 2014-11-06 11:12
    已采纳

    Sorry for the late reply.

    The solution I chose was to use a service reference and add the headers to the web.config like this:

    <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="APIServiceBinding" />
          </basicHttpBinding>
        </bindings>
        <client>
          <endpoint address="http://api.sendcockpit.com/server.php" binding="basicHttpBinding"
      bindingConfiguration="APIServiceBinding" contract="Newsletter.APIServicePortType"
      name="OrdersPort" >
            <headers>
              <validate>
                <apikey>[yourAPIKey]</apikey>
                <userid>[yourUserId]</userid>
                <version>1.0</version>
                <mode>live</mode>
              </validate>
            </headers>
          </endpoint>
        </client>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    </system.serviceModel>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongxie1907 2014-09-26 08:25

    Below code is sample to do webservice call with SOAP.

        {
            WebRequest request = WebRequest.Create(url); //Pass your Url here
            request.Method = "POST";
            request.ContentType = "text/xml;charset=UTF-8";
            request.Headers[HttpRequestHeader.Authorization] ="Authentication";  //Headers can be added
            request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
        }
    
        void GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
            Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
            string postData = postXml;                 //Xml to be posted
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            postStream.Write(byteArray, 0, byteArray.Length);
            postStream.Close();
            webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
        }
    
        void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
                HttpWebResponse response;
                response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(streamResponse);
                string Response = streamReader.ReadToEnd();  // Response Xml will be available here
                streamResponse.Close();
                streamReader.Close();
                response.Close();
    
            }
            catch
            {
    
            }
        }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题