我刚刚开始拦截对我的WCF服务的请求。
我正在使用看起来像这样的短代码(短版本)来调用Web服务
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Username", "Testname");
我收到了请求,但我无法在邮件请求中获取/找到标头。我尝试过这样的事情:
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方面做错了吗?
答案 0 :(得分:15)
Headers
类中的Message
属性会为您提供 SOAP 标头;你要找的是 HTTP 标题。要获得这些,您应该使用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;
}