在现有的WCF SOAP服务中添加Http标头不起作用 [英] Adding Http Header in an existing WCF SOAP Service is not working
问题描述
我想要WCF SOAP服务的HTTP标头.我的最终目标是通过此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://****.com/**/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标头
要添加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
}
然后,服务器端,使用以下方法进行抓取:
And then, server-side, grab it using:
MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;
string identity = headers.GetHeader<string>("User-Auth", ns);
NB. 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屋!