none
How to properly add HttpRequestHeaders using system.net.http library RRS feed

  • Question

  • Hi All,

    I created a HttpRequestMessage with header using windows.web.http; library:

    var request = base.CreateHttpRequestMessage();
                request.Headers.Add("SESSION", SessionId);
                return request;

    and after sending HttpClient SendRequestAsync(); , I am able to get the response successfully .

    But in my case I need to use system.net.http; library, with same codes:

    var request = base.CreateHttpRequestMessage();
                request.Headers.Add("SESSION", SessionId);
                return request;

    but when I am calling the HttpClient SendAsync(); , I am getting an UnAuthorized response.
    Does anyone knows what causing the issue?

    var source = new CancellationTokenSource(timeout)
    var response = await _client.SendAsync(request, source.Token)
                            .ConfigureAwait(false);

    NOTE: I tried manually setting same valid SessionId for both , on windows.web.http its Okay, but on system.net.http its unauthorized.

    How I initialize my HttpClient:

    private HttpClientHandler handler;
    handler = new HttpClientHandler();
    
    private HttpClient _client;
    _client = new HttpClient(handler)



    Reference Link: https://docs.microsoft.com/en-us/dotnet/api/system.net.http?view=netframework-4.7.2
    https://docs.microsoft.com/en-us/uwp/api/windows.web.http

    Requests Header format:
    windows.web.http: 

    system.net.http:

    Thanks.









    • Edited by NicoTing Thursday, December 13, 2018 7:50 AM
    Tuesday, December 11, 2018 5:19 AM

Answers

  • After debugging the request using Fiddler, I found out that the cookie of my request is null, so I fixed this by adding the cookie instead of adding the header.

    CookieContainer.Add(request.RequestUri, new Cookie("SESSION", SessionId));

    • Marked as answer by NicoTing Monday, December 17, 2018 7:14 AM
    Monday, December 17, 2018 7:14 AM

All replies

  • Use Fiddler to watch the request go by. The header is most likely there since you are explicitly creating the request message. We do this all the time and it works. I wonder if there is actually something else going on like you're missing another header. 

    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, December 11, 2018 3:43 PM
    Moderator
  • could you please elaborate more about the Fiddler? also I updated the question, I added a screenshot of my request header for both library, And some says it might be because of the Cache-Control
    Wednesday, December 12, 2018 12:41 AM
  • Fiddler is a useful tool that you install on your machine. It allows you to monitor network traffic including request/response. It is a critical tool in diagnosing things like this.

    Looking at your 2 requests I see a couple of differences.

    1) The UWP version headers look correct. The NET version does not. It almost looks like a header within a header (notice the double curly). Not seen this before.

    2) The UWP version has a COM property. Did you set that or is it an implementation detail?

    Using fiddler to see the actual request/response would show you the differences between these two requests moreso then using the debugger. It'll also show HTTPS information. You didn't post all the code around the client setup so it could be something else that is different between the two.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, December 12, 2018 3:29 AM
    Moderator
  • Hi @CoolDadTx,

    the double curly bracket one is the working one.
    the COM property it is an implementation detail

    Thanks

    • Edited by NicoTing Wednesday, December 12, 2018 6:54 AM
    Wednesday, December 12, 2018 4:28 AM
  • After debugging the request using Fiddler, I found out that the cookie of my request is null, so I fixed this by adding the cookie instead of adding the header.

    CookieContainer.Add(request.RequestUri, new Cookie("SESSION", SessionId));

    • Marked as answer by NicoTing Monday, December 17, 2018 7:14 AM
    Monday, December 17, 2018 7:14 AM