【问题标题】:GET request yields no response when using RestSharp but gets response in PostmanGET 请求在使用 RestSharp 时没有响应,但在 Postman 中得到响应
【发布时间】:2021-04-14 08:59:29
【问题描述】:

首先,这不是我的日常工作,所以我可能会对某些术语感到困惑。

但我需要在 C# 中进行休息调用。 我有 2 个可以在 Postman 中导入的 JSON 文件。 当我运行第一个时,它会以不记名令牌进行响应。 我必须在第二个中使用该不记名令牌。当我在 Postman 中运行那个时,我会收到一些结果。

现在我需要在 C# 中做同样的事情。 我让 Postman 为第一个 JSON 文件生成代码,该文件检索令牌。将代码粘贴到 Visual Studio 中,它工作并给了我令牌。

然后我还让邮递员为检索数据的 JSON 文件创建 RestSharp 代码(通过使用有效令牌)。当我将那个复制到 Visual Studio 中时,它会给出一个空白结果。

在我正在使用的代码下方:

private void btnGetNLDCountry_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Start");
        var client = new RestClient("https://urlcomeshere.com/data/AddressCountryRegions?$filter=CountryRegion eq 'NLD'");
        client.Timeout = -1;
        var request = new RestRequest(Method.GET);
        request.AddHeader("Authorization", "Bearer tokenComesHere");
        IRestResponse response = client.Execute(request);

        MessageBox.Show(response.IsSuccessful.ToString());
        MessageBox.Show(response.Content);
        txtNLDCountry.Text = response.Content;
        MessageBox.Show("aa");
    }

response.IsSuccesful 为“假”。

response.Content 什么都不是(空字符串)。

我尝试添加某种等待功能,但这并没有解决它,我也不确定这是否是解决它的好方法:

private async void btnGetNLDCountry_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Start");
        var client = new RestClient("https://urlcomeshere.com/data/AddressCountryRegions?$filter=CountryRegion eq 'NLD'");
        client.Timeout = -1;
        var request = new RestRequest(Method.GET);
        request.AddHeader("Authorization", "Bearer tokenComesHere");
        IRestResponse response = client.Execute(request);

        MessageBox.Show(response.IsSuccessful.ToString());
        while (!response.IsSuccessful)
        {
            await Task.Delay(1000);
        }

        MessageBox.Show(response.Content);
        txtNLDCountry.Text = response.Content;

    }

有谁知道为什么它在 Postman 中工作,但在我的 C# 代码中却没有?

【问题讨论】:

  • 错误代码是什么?如果response.IsSuccessful 为假,那么应该有一个 ErrorCode != 2XX
  • 顺便说一句:如果返回不成功,则无需等待。它将保持不成功:D
  • "access_token":"System.Net.WebException:底层连接已关闭:发送时发生意外错误。---> System.IO.IOException:无法从传输中读取数据connection: 已存在的连接被远程主机强行关闭。---> System.Net.Sockets.SocketException: 已存在的连接被远程主机强行关闭
  • 似乎是关于令牌的问题......也许使用wireshark来看看实际发生了什么?
  • @Fildor,似乎与 TLS 有关...刚刚修复它。感谢您提示我输入错误代码!

标签: c# rest postman restsharp


【解决方案1】:

我刚刚在另一篇文章中找到了答案。我查找了错误文本和堆栈溢出的某个地方,我发现我必须使用:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

所以我的代码现在看起来像这样:

private void btnGetNLDCountry_Click(object sender, EventArgs e)
{
    MessageBox.Show("Start");
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    var client = new RestClient("https://urlcomeshere.com/data/AddressCountryRegions?$filter=CountryRegion eq 'NLD'");
    client.Timeout = -1;
    var request = new RestRequest(Method.GET);
    request.AddHeader("Authorization", "Bearer tokenComesHere");
    IRestResponse response = client.Execute(request);

    MessageBox.Show(response.IsSuccessful.ToString());
    MessageBox.Show(response.Content);
    txtNLDCountry.Text = response.Content;
    MessageBox.Show("aa");
}

感谢您提示我有关错误代码的信息!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    • 2023-03-23
    • 2020-07-06
    • 2019-09-10
    • 1970-01-01
    相关资源
    最近更新 更多