1

我正在尝试使用第 3 方 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);

在 Fiddler 中查看数据包后,我注意到 Auth 标头从未发送到服务器。有没有办法在我的请求中手动插入授权标头?

4

1 回答 1

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 请求中获取自定义标头的最不脏的方法。如果有人有更好的方法,我很想听听。

于 2013-02-28T04:24:00.697 回答