wcf 服务器端消息头,如何:设置客户端请求(WCF 数据服务)中的标头

本文介绍如何在WCF数据服务客户端库中处理SendingRequest事件,以在发送到数据服务的请求中手动设置HTTP标头,特别是对于需要身份验证或Cookie的情况。示例代码展示了如何添加授权标头。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何:设置客户端请求(WCF 数据服务)中的标头

10/24/2013

本文内容

使用 WCF 数据服务客户端库访问支持 开放式数据协议 (OData) 的数据服务时,客户端库会自动在发送给数据服务的请求消息中设置所需 HTTP 标头。 但是,在某些情况下客户端库不知道要设置所需的消息标头,例如当数据服务要求基于声明的身份验证或 Cookie 时。 有关更多信息,请参见 Securing WCF Data Services。 这时,必须先在请求消息中手动设置消息标头,然后再发送消息。 本主题中的示例揭示了如何处理 SendingRequest 事件以便在将请求消息发送至数据服务之前在其中添加新标头。

本主题中的示例使用罗斯文示例数据服务和自动生成的客户端数据服务类。 此服务和这些客户端数据类是在完成 WCF 数据服务快速入门时创建的。 还可以使用 OData 网站上发布的罗斯文示例数据服务;该示例数据服务是只读的,它会尝试保存更改并返回错误。 OData 网站上的示例数据服务允许进行匿名身份验证。

示例

以下示例为 SendingRequest 事件注册一个处理程序,然后执行针对数据服务的查询。

备注

如果数据服务要求为每个请求手动设置消息标头,则考虑在代表数据服务的实体容器(在本例中为 NorthwindEntities)中覆盖 OnContextCreated 分部方法,以此注册 SendingRequest 事件的处理程序。

' Create the DataServiceContext using the service URI.

Dim context As NorthwindEntities = New NorthwindEntities(svcUri)

' Register to handle the SendingRequest event.

' Note: If this must be done for every request to the data service, consider

' registering for this event by overriding the OnContextCreated partial method in

' the entity container, in this case NorthwindEntities.

AddHandler context.SendingRequest, AddressOf OnSendingRequest

' Define a query for orders with a Freight value greater than 30.

Dim query = From cust In context.Customers _

Where cust.Country = "Germany" _

Select cust

Try

' Enumerate to execute the query.

For Each cust As Customer In query

Console.WriteLine("Name: {0}" & vbNewLine & "Address:" & vbNewLine & "{1}" _

& vbNewLine & "{2}, {3}", _

cust.CompanyName, cust.Address, cust.City, cust.Region)

Next

Catch ex As DataServiceQueryException

Throw New ApplicationException( _

"An error occurred during query execution.", ex)

End Try

// Create the DataServiceContext using the service URI.

NorthwindEntities context = new NorthwindEntities(svcUri);

// Register to handle the SendingRequest event.

// Note: If this must be done for every request to the data service, consider

// registering for this event by overriding the OnContextCreated partial method in

// the entity container, in this case NorthwindEntities.

context.SendingRequest += new EventHandler(OnSendingRequest);

// Define a query for orders with a Freight value greater than 30.

var query = from cust in context.Customers

where cust.Country == "Germany"

select cust;

try

{

// Enumerate to execute the query.

foreach (Customer cust in query)

{

Console.WriteLine("Name: {0}\nAddress:\n{1}\n{2}, {3}",

cust.CompanyName, cust.Address, cust.City, cust.Region);

}

}

catch (DataServiceQueryException ex)

{

throw new ApplicationException(

"An error occurred during query execution.", ex);

}

以下方法将处理 SendingRequest 事件并为请求添加一个身份验证标头。

Private Shared Sub OnSendingRequest(ByVal sender As Object, ByVal e As SendingRequestEventArgs)

' Add an Authorization header that contains an OAuth WRAP access token to the request.

e.RequestHeaders.Add("Authorization", "WRAP access_token=""123456789""")

End Sub

private static void OnSendingRequest(object sender, SendingRequestEventArgs e)

{

// Add an Authorization header that contains an OAuth WRAP access token to the request.

e.RequestHeaders.Add("Authorization", "WRAP access_token=\"123456789\"");

}

请参阅

概念

其他资源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值