.net core HTTPS 请求返回 502 bad gateway 而 Postman 返回 200 OK [英] .net core HTTPS requests returns 502 bad gateway while Postman returns 200 OK

查看:88
本文介绍了.net core HTTPS 请求返回 502 bad gateway 而 Postman 返回 200 OK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#.NET core 3 中的这段代码有什么问题:

What's wrong with this code snippet in 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());
            }
        }
    }
}

当在 response.EnsureSuccessStatusCode() 行使用断点运行它时,我总是得到 502 响应.但是,如果在 Postman 或 curl 中运行它,我会得到一个有效的结果.

When running this with a breakpoint at the line response.EnsureSuccessStatusCode(), I always get 502 response. However, if running this in Postman or in curl, I got back a valid result.

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 所以代替 StringContent 使用 >FormUrlEncodedContent:

The request uses application/x-www-form-urlencoded so instead of 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);

这篇关于.net core HTTPS 请求返回 502 bad gateway 而 Postman 返回 200 OK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆