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

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());
}

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>

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
        {

        }
    }