gpt4 book ai didi

c# - 将 HTTP 请求 header 添加到 WCF 请求

转载 作者:太空狗 更新时间:2023-10-29 17:33:20 27 4
gpt4 key购买 nike

我有一个由 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com