【问题标题】:GET API call works in browser but not in Postman and HttpClient C#GET API 调用在浏览器中有效,但在 Postman 和 HttpClient C# 中无效
【发布时间】:2021-09-19 17:55:46
【问题描述】:

这是网址:https://thongtindoanhnghiep.co/api/company/3901212654

用于导入 Postman 的 curl:

curl --location --request GET 'https://thongtindoanhnghiep.co/api/company/3901212654' \
--header 'sec-ch-ua: " Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"' \
--header 'sec-ch-ua-mobile: ?0' \
--header 'Upgrade-Insecure-Requests: 1' \
--header 'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' \
--header 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
--header 'Sec-Fetch-Site: none' \
--header 'Sec-Fetch-Mode: navigate' \
--header 'Sec-Fetch-User: ?1' \
--header 'Sec-Fetch-Dest: document'

以及C#中的代码

// Create client to get API from thongtindoanhnghiep
HttpClient client = new HttpClient();

// Get company info
var stringTask = client.GetStringAsync("https://thongtindoanhnghiep.co/api/company/3901212654");
var msg = await stringTask;
var parsedCompany = JsonConvert.DeserializeObject<CompanyDto.ParseFromApi>(msg);

Postman 和 C# HttpClient 都不起作用。 邮递员会抛出“不能发送请求”,而 HttpClient 会抛出“不知道这样的主机”。 但浏览器工作正常

有人可以帮帮我吗?

更新: 这似乎是网络错误或因为 ISP。我更改为具有不同 ISP 的另一个网络,它工作正常。我稍后会检查我的网络设置。感谢您的帮助!

【问题讨论】:

  • 您是否查看过 chrome 工具-> 网络选项卡并将正在拨打的电话与邮递员发送的电话进行比较?
  • 你能添加一张你在邮递员中所做的截图吗?我刚刚在邮递员中对该 URL 进行了 GET 请求,它按预期返回了数据。
  • 我也在 Postman 中尝试过,一切正常。它也使用 httpClient 返回数据。
  • 检查您的代理设置。

标签: c# api postman


【解决方案1】:

尝试使用 get() 或 getAsync()。

对于 GET 方法

           var URL = https://thongtindoanhnghiep.co/api/company/3901212654";
           HttpClient clients = new HttpClient();
           clients.BaseAddress = new Uri(URL);
           clients.DefaultRequestHeaders.Accept.Add(
           new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = clients.GetAsync(URL).Result;
            Console.WriteLine(response);
            var res= "";
            if (response.IsSuccessStatusCode)
            {
                res = response.Content.ReadAsStringAsync().Result;
            }

对于 POST 方法(使用 Resharper)

var URL = https://thongtindoanhnghiep.co/api/company/3901212654";
var rootobject = Newtonsoft.Json.JsonConvert.SerializeObject(object); 
//object to string
var dataObject = "";

var client = new RestClient(URL);
var request = new RestRequest();
request.Method = Method.POST;
request.AddHeader("Accept", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json", rootobject, ParameterType.RequestBody);

var response = client.Execute(request);
if (response.IsSuccessful)
    {
       dataObject = response.Content; // raw content as string  
    }

如果 HttpClient 不起作用,请使用 WebClient

【讨论】:

    猜你喜欢
    • 2016-07-11
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 2021-04-03
    • 2020-04-06
    • 2019-03-20
    • 2023-02-01
    • 1970-01-01
    相关资源
    最近更新 更多