c# - .net core HTTPS 请求返回 502 bad gateway 而 Postman 返回 200 OK
问题描述
C#.NET core 3 中的这段代码有什么问题:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static async Task Main(string[] args)
{
var uriBuilder = new UriBuilder
{
Scheme = Uri.UriSchemeHttps,
Host = "api.omniexplorer.info",
Path = "v1/transaction/address",
};
var req = new Dictionary<string, string>
{
{ "addr", "1FoWyxwPXuj4C6abqwhjDWdz6D4PZgYRjA" }
};
using(var httpClient = new HttpClient())
{
var response = await httpClient.PostAsync(uriBuilder.Uri, new StringContent(JsonConvert.SerializeObject(req)));
response.EnsureSuccessStatusCode();
Console.WriteLine(response.Content.ToString());
}
}
}
}
在 line 处使用断点运行它时response.EnsureSuccessStatusCode()
,我总是得到 502 响应。但是,如果在 Postman 或 curl 中运行它,我会得到一个有效的结果。
卷曲中的示例:
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "addr=1EXoDusjGwvnjZUyKkxZ4UHEf77z6A5S4P" "https://api.omniexplorer.info/v1/transaction/address"
非常感谢您帮助新手!
解决方案
请求使用application/x-www-form-urlencoded
so 而不是StringContent
use FormUrlEncodedContent
:
var content = new FormUrlEncodedContent(req);
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
var response = await httpClient.PostAsync(uriBuilder.Uri, content);
推荐阅读
- python - 使用 Python 作为 root 用户更新 UNIX 帐户密码的安全方法?
- vue.js - 如何将表单输入的当前值传递给 Vue 组件道具?
- amazon-web-services - AWS Lambda 函数无法发送电子邮件
- python - 在 Python 中估计相关性
- discord - Discord.js - 当有人获得特定角色时触发事件的任何方式?
- ios - iphone应用程序进入后台时注册回调的C API
- firebase - Flutter:数组的 Firebase 映射
- python - 试图做一个错误检查器来检查字符串中是否有一个数字,但它永远不会起作用
- powershell - 将文件中的字符串作为单独的变量存储在 Powershell 中
- django - 一个数据库的两个 Django 项目