Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF AfterReceiveRequest get headers

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?

like image 257
Lucas Arrefelt Avatar asked Sep 23 '13 14:09

Lucas Arrefelt


1 Answers

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;
    }
like image 191
carlosfigueira Avatar answered Sep 24 '22 06:09

carlosfigueira