Api returns 导致 fiddler 但响应不是控制台中的结果
Api returns result in fiddler but the response doesn't the result in console
我 运行 .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
}
阅读回复内容
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) {
//...
我 运行 .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
}
阅读回复内容
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) {
//...