WCF AfterReceiveRequest get headers

时光总嘲笑我的痴心妄想 提交于 2019-12-01 02:29:39

问题


I just got started intercepting requests to my WCF service.

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;
}

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?


回答1:


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;
    }


来源:https://stackoverflow.com/questions/18961589/wcf-afterreceiverequest-get-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!