繁体   English   中英

GET 请求在使用 RestSharp 时没有响应,但在 Postman 中得到响应

[英]GET request yields no response when using RestSharp but gets response in Postman

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

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

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

然后我还让 postman 为检索数据的 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 什么都不是(空字符串)。

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

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# 代码中工作?

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

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");
}

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

这对我真的有用 (Y) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

我刚刚在你的帖子中找到了答案。 我查找了错误文本并在堆栈溢出时发现我必须使用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM