2012-09-25 53 views
2

我有一个正从一个iPhone应用程序名为WebService的(即我还建设)如何阅读HTTP标头值

在我的web服务时它正在自我服务承载的,它是一切运作良好,除了我想将安全令牌移入请求的头部以便类对象保持整齐。 (如果我不能把它放在标题中,我会采取把它放在课堂上,但这有点丑陋imo)。

我已经看过这个http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.incomingmessageheaders.aspx#Y342的代码,我似乎无法枚举标题值。

展望提琴手,我可以看到头正在通过

POST http://192.168.1.221:11001/StockControl/json/SubmitResults HTTP/1.1 
Device-Token: bwI2YiAHR4q3Ba5JVj99Cw== 
Content-Type: application/json 
Content-Length: 1663 
User-Agent: StockManage/1.0 CFNetwork/609 Darwin/12.1.0 

通过我不知道如果我没有设置我的SelfHosted配置正确,或者如果我还没有实现一个必要的接口。

WCF IClientMessageInspector and the incoming SOAP headers但这是使用SOAP,我正在使用JSON。

我的终点是设置使用以下

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

最后,在我的SubmitResults功能在我的服务实现

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

但指数始终是-1(未找到)和WriteHeaders看不到头。

+0

如果您的服务仅通过HTTP运行,并且您只使用json端点,则可能需要查看Web API(http://www.asp.net/web-api) 。它会让你的生活更轻松。 –

+0

我可能应该补充说我有其他的终端正在使用。这只是我只显示了JSON,也是在一个Service中运行。没有IIS等涉及。 –

+0

认为可能是这样的情况。希望有人知道答案! –

回答

2

经过大量的搜索,我相信我在这里找到了答案。 (http://social.msdn.microsoft.com/Forums/pl-PL/wcf/thread/72ee44cc-58bb-45b2-aff7-49d9bbc8176e)

HttpRequestMessageProperty reqMsg = 
      OperationContext.Current.IncomingMessageProperties["httpRequest"] as 
      HttpRequestMessageProperty; 
0

这对我的作品...其中apiKey是标题名称

> var headers =OperationContext.Current.IncomingMessageProperties["httpRequest"]; 
      var apiToken = ((HttpRequestMessageProperty)headers).Headers["apiKey"];