问题描述
限时送ChatGPT账号..我想要一个 HTTP 标头到我的 WCF SOAP 服务.我的最终目标是通过这个 HTTP 标头发送 API 密钥.
I would like to an HTTP header to my WCF SOAP Service. My end goal is to send API keys through this HTTP header.
下面是我的代码:
[ServiceBehavior(Namespace = "http://****/**/1.1")]
public class MyWcfSvc : IMyVerify
{
const int MaxResponseSize = 0xffff; // 64K max size - Normally it will be MUCH smaller than this
private static readonly NLogLogger Logger;
static MyWcfSvc()
{
Logger = new NLogLogger();
// Add an HTTP Header to an outgoing request
HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
requestMessage.Headers["User-Auth"] = "MyHttpHeaderValue";
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
}
}
我在 HTTP 请求标头下没有看到 User-Auth
标头.
I do not see User-Auth
header under HTTP request headers.
我也尝试过另一种方式.
I also tried with another way.
public AnalyzeResponse Analyze(AnalyzeRequest analyzeRequest)
{
HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
requestMessage.Headers["User-Auth"] = "MyHttpHeaderValue";
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
.
.
. Rest of the service implementation
.
.
.
}
但是,我仍然没有在请求消息中看到任何 HTTP 标头信息.我正在使用 SOAP UI 发送请求并查看响应.
But, still, I don't see any HTTP header information with the request message. I am using SOAP UI to send the requests and to view the responses.
我该怎么办?我是否应该对与课程相关的服务进行更改?或者我需要对 web.config
文件进行一些更改?
How should I go about this? Am I suppose to make changes to the Service related to class? Or I need to make some changes to web.config
file?
推荐答案
SOAP Header
要添加 SOAP 标头,请使用以下代码客户端:
To add a SOAP header, use the following code client-side:
using (OperationContextScope scope = new OperationContextScope((IContextChannel)channel))
{
MessageHeader<string> header = new MessageHeader<string>("MyHttpHeaderValue");
var untyped = header.GetUntypedHeader("User-Auth", ns);
OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
// now make the WCF call within this using block
}
然后,服务器端,使用:
MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;
string identity = headers.GetHeader<string>("User-Auth", ns);
注意.ns
是头部 XML 元素的命名空间 URI.
HTTP 标头
添加一个 Http 头:
To add an Http header:
// Add a HTTP Header to an outgoing request
HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
requestMessage.Headers["MyHttpHeader"] = "MyHttpHeaderValue";
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
并抓取它服务器端
IncomingWebRequestContext request = WebOperationContext.Current.IncomingRequest;
WebHeaderCollection headers = request.Headers;
Console.WriteLine(request.Method + " " + request.UriTemplateMatch.RequestUri.AbsolutePath);
foreach (string headerName in headers.AllKeys)
{
Console.WriteLine(headerName + ": " + headers[headerName]);
}
这篇关于在现有 WCF SOAP 服务中添加 Http 标头不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论