1

At the moment I am using the following RestSharp request to get a website's content:

        var client = new RestClient(productLink);
        var request = new RestRequest(Method.GET);
        request.AddHeader("Cookie", "insert-cookie-content"); 
        IRestResponse response = client.Execute(request);
        Console.WriteLine(response.Content);

I have tried converting it into HttpClient as i will need to use the AllowRedirect property later:

        var client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Add("Cookie", "insert-cookie-content");
        var response = await client.GetAsync(productUrl);
        Console.WriteLine(response);

The URL I am trying to get a response from is: https://www.nike.com/sg/launch/t/air-max-90-orange-duck-camo

My first problem is that the HttpClient request is giving me 403 Errors whereas the RestClient request was working fine. How can I fix this?

My second problem is that the cookie expires after a couple of uses, and I have to manually get a new one from postman and insert it. Is there anyway for the request to generate its own cookie?

Here is the two fiddler responses compared: https://i.sstatic.net/UK2Lo.jpg

7
  • 1
    Have you compared the two outgoing requests? Like capturing them with Fiddler (or some similar tool) and compare them side-by-side. Commented Jun 16, 2020 at 11:57
  • @PeterCsala I have, doesn't make much sense to me but I can see that the RestSharp request has some security headers whereas the HttpClient does not. Commented Jun 16, 2020 at 12:14
  • 1
    Could please include the result of your comparison into the question? So, which ones are missing from the HttpClient based solution. Commented Jun 16, 2020 at 12:17
  • 1
    I meant the request headers, not the response. :D Commented Jun 16, 2020 at 13:35
  • 1
    A web server does not care what library is being used to call it. All the web server sees are the requests. So if you compare the requests, find out what's different about them, and make them the same, then the server will treat them the same. That's why it was suggested to use Fiddler. Commented Jun 16, 2020 at 14:36

1 Answer 1

1

In case of HttpClient if you want to pass the Cookies manually through the DefaultRequestHeaders then you have to tell this to the HttpClient to do NOT use CookieContainer. You have to use HttpClientHandler's UseCookie flag to indicate it.

var client = new HttpClient(new HttpClientHandler { UseCookies = false });
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("Cookie", "insert-cookie-content");
var response = await client.GetAsync(productUrl);
Console.WriteLine(response);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.