How to read HTTP request headers in a WCF web service?

前端 未结 2 1236
囚心锁ツ
囚心锁ツ 2020-12-05 04:10

In a WCF web service, how does one read an HTTP/HTTPS request header? In this case, i\'m trying to determine the original URL host the client used. This might be in the X-Fo

2条回答
  •  独厮守ぢ
    2020-12-05 04:50

    Try WebOperationContext.Current.IncomingRequest.Headers

    I use following codes to see all headers :

    IncomingWebRequestContext request = WebOperationContext.Current.IncomingRequest;
    WebHeaderCollection headers = request.Headers;
    
    Console.WriteLine("-------------------------------------------------------");
    Console.WriteLine(request.Method + " " + request.UriTemplateMatch.RequestUri.AbsolutePath);
    foreach (string headerName in headers.AllKeys)
    {
      Console.WriteLine(headerName + ": " + headers[headerName]);
    }
    Console.WriteLine("-------------------------------------------------------");
    

提交回复
热议问题