2

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 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 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://learn.microsoft.com/en-us/dotnet/api/system.net.http?view=netframework-4.7.2
https://learn.microsoft.com/en-us/uwp/api/windows.web.http

Requests Header format:
windows.web.http: enter image description here system.net.http: enter image description here

Thanks.

  • About `use system.net.http`, do you mean to use `system.net.http.HttpClient`? – Jerry Liu Dec 11 '18 at 06:17
  • @JerryLiu yes... –  Dec 11 '18 at 06:19
  • So you have written code using HttpClient but got UnAuthorized? Could you post your code using HttpClient, on my side I could get Session header successfully. – Jerry Liu Dec 11 '18 at 06:22
  • when calling HttpClient.SendAsync(); , our api service returns unauthorized, even I added the valid session on the header –  Dec 11 '18 at 06:30
  • updated the question , attached the screenshot of the request –  Dec 11 '18 at 06:43
  • Is it possible the problem is related to how the api service handles the request? – Jerry Liu Dec 14 '18 at 06:39
  • @JerryLiu , yes its possible, but what I am confused is why on windows.web.http library, its working fine. –  Dec 14 '18 at 06:47

2 Answers2

1

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

One disadvantage of system.net.http is that doesn't cache requests. Server that doesn't send Cache-Control can be potential problem.

Laci R
  • 31
  • 1
  • 7