2013-02-28 46 views
1

我試圖消費第三方WSDL。我已經添加它作爲服務參考。我initalize客戶端和查詢PARAMATERS這樣的:消費需要C#中的動作級別授權的WSDL

var ltRequest = new SearchEmailAddressStatus 
      { 
       EmailAddress = emailAddressList.ToArray() 
      }; 
var ltClient = new CommunicationPreferenceServiceClient 
      { 
       ClientCredentials = 
        { 
         UserName = 
         { 
          UserName = ltProperties.CompanyCredential.UserName, 
          Password = ltProperties.CompanyCredential.Password 
         } 
        } 
       }; 
var ltResponse = ltClient.searchEmailAddressStatusWS(ltRequest); 

看着提琴手報文後,我注意到了驗證頭永遠不會發送到服務器。有沒有辦法在我的請求中手動插入授權標頭?

回答

0

好的,經過大量的挖掘,我找到了答案。聲明客戶端之後,我用下面的代碼:

using (var scope = new OperationContextScope(ltClient.InnerChannel)) 
{ 
    var reqProperty = new HttpRequestMessageProperty(); 
    reqProperty.Headers[HttpRequestHeader.Authorization] = "Basic " 
      + Convert.ToBase64String(Encoding.ASCII.GetBytes(
       ltClient.ClientCredentials.UserName.UserName + ":" + 
       ltClient.ClientCredentials.UserName.Password)); 

    OperationContext.Current 
     .OutgoingMessageProperties[HttpRequestMessageProperty.Name] = reqProperty; 

    var ltResponse = ltClient.searchEmailAddressStatusWS(ltRequest); 
} 

相信這是獲得WSDL請求內的定製報頭的至少髒的裝置。如果有人有更好的方法,我很樂意聽到它。

+1

不,這幾乎是我如何做到的。我希望你使用傳輸級安全性,因爲你傳遞的是憑據。 – 2013-02-28 04:44:19

+0

好的電話,我。 – Billdr 2013-02-28 05:01:25