Paul Farry Paul Farry - 1 year ago 103
JSON Question

How to Read HTTP header value

I have a WebService that is being called from an Iphone app (that I am also building)

In my webservice is it being self hosted inside a Service and it is all working well, except I would like to move a security token into the Headers of the Request so that the class objects remain neat. (If I can't get it in the header, i'll resort to putting it in the class but that's a bit ugly imo).

I have looked at the code in this and I can't seem to enumerate the header value.

Looking in Fiddler, I can see the header is being passed through

Device-Token: bwI2YiAHR4q3Ba5JVj99Cw==
Content-Type: application/json
Content-Length: 1663
User-Agent: StockManage/1.0 CFNetwork/609 Darwin/12.1.0

I'm not sure if I haven't set up my SelfHosted configuration correctly or if I haven't implemented a necessary interface .

WCF IClientMessageInspector and the incoming SOAP headers but this is using SOAP and I'm using JSON.

My Endpoint is setup using the following

WebHttpBinding jsonBind = new WebHttpBinding();
ServiceEndpoint jsonServer = host.AddServiceEndpoint(typeof(POSServer.StockControl.IStockService), jsonBind, "json");

jsonServer.Behaviors.Add(new WebHttpBehavior
DefaultBodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Bare,
HelpEnabled = true,
DefaultOutgoingResponseFormat = WebMessageFormat.Json

Finally in my SubmitResults function in my Service implementation

public bool SubmitResults(Business.StockResultData theData)
DateTime uploadTime = DateTime.Now;
int index = OperationContext.Current.IncomingMessageHeaders.FindHeader("Device-Token", "");

but index is always -1 (not found) and the WriteHeaders cannot see the header.

Answer Source

After a lot of searching I believe I found the answer here . (

HttpRequestMessageProperty reqMsg = 
          OperationContext.Current.IncomingMessageProperties["httpRequest"] as 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download