Ehsan Akbar
Ehsan Akbar

Reputation: 7301

Api returns result in fiddler but the response doesn't the result in console

I run this code in .net core console to call an api :

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var httpClientHandler = new HttpClientHandler())
            {
                httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
                httpClientHandler.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;

                using (var httpClient = new HttpClient())
                {
                    ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) => { return true; };
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "****=");
                    httpClient.DefaultRequestHeaders.Add("PID", "****");
                    var content = new FormUrlEncodedContent(new Dictionary<string, string>
             {
              { "grant_type", "password" },
              { "username", "****" },
              { "password", "****" }
             });

                    HttpResponseMessage response = httpClient.PostAsync("https://myurl", content).Result;

                    Console.WriteLine(response.ToString());
                }
            }
        }
    }
}

When I call the API I check the fiddler to see the response:

enter image description here

But the result of my code is this :

StatusCode: 200, ReasonPhrase: '', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
 {
 requestid: 26d6ac8559744dd59b4ba77c590212e9
 Cache-Control: no-store
 Pragma: no-cache
 Transfer-Encoding: chunked
 Date: Wed, 20 Jan 2021 11:12:33 GMT
 Server: PGSB
 X-Powered-By: PGSB
 X-Frame-Options: DENY
 x-xss-protection: 1;mode=block
 X-Content-Type-Options: nosniff
 Strict-Transport-Security: max-age=31536000;includeSubDomains;preload
 referrer-policy: no-referrer-when-downgrade
 Content-Security-Policy: default-src https:; img-src https: data:; script-src 'self' 'unsafe-eval' 'unsafe-inline'; style-src 'self' 'unsafe-inline' report-uri https://pgsb.iran.gov.ir/actions/reportingEndpointForContentSecurityPolicy
 Connection: close
 Content-Type: application/json; charset=UTF-8

}

Upvotes: 1

Views: 90

Answers (1)

Nkosi
Nkosi

Reputation: 247153

Read the content of the response

HttpResponseMessage response = await httpClient.PostAsync("https://myurl", content);

string json = await response.Content.ReadAsStringAsync();
                
Console.WriteLine(json);

I would also suggest making the code asynchronous and avoid blocking calls like .Result

class Program {
    public static async Task Main(string[] args) {

        //...

Upvotes: 1

Related Questions