Search code examples
c#.net-coredotnet-httpclienthttp-method

Is there a more elegant/clean way to set HttpRequestMessage Headers using TryAddWithoutValidation method?


I'm using HttpRequestMessage from HttpClient in a couple of methods and currently, I'm repeating the following piece of code all over my code:

This code was converted by https://curl.olsh.me/ so I'm not sure if best practices were used here.

// using System.Net.Http;
using (var httpClient = new HttpClient(handler))
 {
     using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://www.url.com/"))
                {
                    request.Headers.TryAddWithoutValidation("authority", "www.url.com");
                    request.Headers.TryAddWithoutValidation("pragma", "no-cache");
                    request.Headers.TryAddWithoutValidation("cache-control", "no-cache");
                    request.Headers.TryAddWithoutValidation("dnt", "1");
                    request.Headers.TryAddWithoutValidation("x-requested-with", "XMLHttpRequest");
                    request.Headers.TryAddWithoutValidation("x-odesk-csrf-token", "19b91748869456a4ae700ffb69077745");
                    request.Headers.TryAddWithoutValidation("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36");
                    request.Headers.TryAddWithoutValidation("accept", "*/*");
                    request.Headers.TryAddWithoutValidation("origin", "https://www.url.com");
                    request.Headers.TryAddWithoutValidation("sec-fetch-site", "same-origin");
                    request.Headers.TryAddWithoutValidation("sec-fetch-mode", "cors");
                    request.Headers.TryAddWithoutValidation("sec-fetch-dest", "empty");
                    request.Headers.TryAddWithoutValidation("referer", "https://www.url.com/");
                    request.Headers.TryAddWithoutValidation("accept-language", "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7,fr-FR;q=0.6,fr;q=0.5");
                    request.Headers.TryAddWithoutValidation("cookie", "G_AUTHUSER_H=1; AccountSecurity_cat=fc4d14f1.oauth2v2_812293");
                    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

                    var response = await httpClient.SendAsync(request);

                    IEnumerable<string> cookies = new List<string>();
                    response.Headers.TryGetValues("Set-Cookie", out cookies);

Solution

  • really appreciate the answers from @Jaquez and @Kuroiyatsu, from both I get to the following

     public async Task<string> postAsync(param1, param2)
    {
        using (var httpClient = new HttpClient(handler))
        {
            using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://www.url.com/"))
            {
    
    ....
    
    var response = await PostAsync(param1, param2);
    
    var variable = JsonSerializer.Deserialize<Obj>(response);
    
    WebScrapFunc(response);
    
    ...
    

    Although it seems weird to return a Task<string> from PostAsync it fits fine my intents.