【问题标题】:Api returns result in fiddler but the response doesn't the result in consoleApi 在提琴手中返回结果,但响应不是控制台中的结果
【发布时间】:2021-01-20 11:51:27
【问题描述】:

我在 .net 核心控制台中运行此代码来调用 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());
                }
            }
        }
    }
}

当我调用 API 时,我会检查提琴手以查看响应:

但是我的代码的结果是这样的:

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

}

【问题讨论】:

    标签: c# .net-core fiddler


    【解决方案1】:

    读取响应内容

    HttpResponseMessage response = await httpClient.PostAsync("https://myurl", content);
    
    string json = await response.Content.ReadAsStringAsync();
                    
    Console.WriteLine(json);
    

    我还建议使代码异步并避免阻塞调用,例如.Result

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      • 2017-04-26
      相关资源
      最近更新 更多