简体   繁体   English

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

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

First of all, this is not my day to day work so I might be confusing with some terms.首先,这不是我的日常工作,所以我可能会对某些术语感到困惑。

But I need to do a rest call in C#.但我需要在 C# 中调用 rest。 I got 2 JSON files which I can import in Postman.我有 2 个 JSON 文件,我可以将其导入 Postman 中。 When I run the first one, it responds with a bearer token.当我运行第一个时,它会以不记名令牌进行响应。 That bearer token I have to use in the second one.我必须在第二个中使用该不记名令牌。 When I run that one in Postman, I receive some results.当我在 Postman 中运行那个时,我收到了一些结果。

Now I need to do the same in C#.现在我需要在 C# 中做同样的事情。 I let Postman generate the code for the first JSON file, which retrieves the token.我让 Postman 为第一个 JSON 文件生成代码,该文件检索令牌。 Pasted the code in Visual Studio and it worked and gave me the token.将代码粘贴到 Visual Studio 中,它工作并给了我令牌。

Then I also let postman create the RestSharp code for the JSON file which retrieves the data (by using a valid token).然后我还让 postman 为检索数据的 JSON 文件创建 RestSharp 代码(通过使用有效令牌)。 When I copy that one into Visual Studio, it gives a blank result.当我将那个复制到 Visual Studio 中时,它会给出一个空白结果。

Below the code I'm using:在我正在使用的代码下方:

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 is "false". response.IsSuccesful 为“假”。

response.Content is nothing (empty string). response.Content 什么都不是(空字符串)。

I tried to add some kind of wait function put this didn't solve it and I'm also not sure if that would be a good way to solve it:我试图添加某种等待 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;

    }

Does anyone have any idea why it is working in Postman but not in my C# code?有谁知道为什么它在 Postman 但不在我的 C# 代码中工作?

I just found the answer on another post.我刚刚在另一个帖子上找到了答案。 I looked up the error text and somewhere on stack overflow I found I have to use:我查找了错误文本和堆栈溢出的某个地方,我发现我必须使用:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

So my code looks like this now:所以我的代码现在看起来像这样:

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

Thanks for hinting me about the error codes!感谢您提示我有关错误代码的信息!

That really work for me (Y) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;这对我真的有用 (Y) ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

I just found the answer on your post.我刚刚在你的帖子中找到了答案。 I looked up the error text and on stack overflow I found I have to use我查找了错误文本并在堆栈溢出时发现我必须使用

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

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