WCF AfterReceiveRequest 获取标头 [英] WCF AfterReceiveRequest get headers
问题描述
我刚刚开始拦截对我的 WCF 服务的请求.
I just got started intercepting requests to my WCF service.
我正在使用如下所示的 Java 代码调用 Web 服务(简短版本)
I'm calling the web service with java code that looks like this ( short version )
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Username", "Testname");
我收到了请求,但我无法获取/找到消息请求中的标头.我试过这样的事情:
I'm receiving the request but I cant get/find the headers in the message request. I've tried something like this:
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
int headerIndex = request.Headers.FindHeader("Username", string.Empty);
var username = request.Headers["Username"]
return null;
}
但我总是以 -1 或异常结束.这样做的正确方法是什么?我在 Java 方面也做错了吗?
But I always end up with -1 or exceptions. What is the right way to do this? Am I doing it wrong on the Java side as well?
推荐答案
Message
类中的 Headers
属性将为您提供 SOAP标题;您正在寻找的是 HTTP 标头.要获得这些,您应该使用 HttpRequestMessageProperty
:
The Headers
property in the Message
class will give you the SOAP headers; what you're looking for are the HTTP headers. To get to those, you should use the HttpRequestMessageProperty
:
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
var prop = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
var userName = prop.Headers["Username"];
return null;
}
这篇关于WCF AfterReceiveRequest 获取标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!