WCF AfterReceiveRequest get headers
Asked Answered
C

1

7

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?

Camp answered 23/9, 2013 at 14:16 Comment(0)
C
17

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;
    }
Cerise answered 23/9, 2013 at 23:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.