我有一个由 AJAX 和 C# 应用程序使用的 WCF 服务,
我需要通过 HTTP 请求 header 发送一个参数。
在我的 AJAX 上,我添加了以下内容并且它有效:
$.ajax({
type: "POST",
url: this.tenantAdminService,
beforeSend: function (req, methodName)
{
req.setRequestHeader("AdminGUID", adminGuid);
}
在 WCF 服务器端,我执行以下操作以获取 header :
string adminGUID = System.Web.HttpContext.Current.Request.Headers["AdminGUID"];
C# 的等价物是什么? 如何发送也将由我的 WCF 服务器使用的 http 请求 header ?
我需要将参数添加到 HTTP 请求 header 而不是消息 header ,
谢谢!
最佳答案
最简单的方法是使用 WebOperationContext通过以下方式:
Service1Client serviceClient = new Service1Client();
using (new System.ServiceModel.OperationContextScope((System.ServiceModel.IClientChannel)serviceClient.InnerChannel))
{
System.ServiceModel.Web.WebOperationContext.Current.OutgoingRequest.Headers.Add("AdminGUID", "someGUID");
serviceClient.GetData();
}
取自此post
关于c# - 将 HTTP 请求 header 添加到 WCF 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13856362/