简体   繁体   English

如何在C#中消费多个REST API

[英]How to consume multiple REST API in C#

Initially, I have consumed a single REST API request in C# and desearilsed the JSON response.最初,我在 C# 中消费了一个 REST API 请求并清除了 JSON 响应。 I want to consume multiple API(2 or 3).我想使用多个 API(2 或 3)。 How do I modify my code for that?我该如何修改我的代码?

static void Main(string[] args)
{
    api1();
}

public static void api1()
{
    var client = new RestClient("https://dummy.restapiexample.com/api/");
    var request = new RestRequest("Data");
    var response = client.Execute(request);

    if (response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        string rawResponse = response.Content;
        var root = JsonConvert.DeserializeObject<Rootobject>(rawResponse)
    }
}

I tried to create function for each API request but I am not sure what will be the return type and how am i going to call all the functions.我尝试为每个 API 请求创建 function,但我不确定返回类型是什么以及我将如何调用所有函数。

public async Task<Var> api2()
{
    var client = new RestClient("https://dummy.restapiexample2.com/api2/");
    var request = new RestRequest("Data");
    var response = client.Execute(request);

    if (response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        string rawResponse = response.Content;
        var root = JsonConvert.DeserializeObject<Rootobject>(rawResponse)
        return root;
    }
    else
        return null;
}

static void Main(string[] args)
{
    api1();
    api2();
}

You can't return var as you have done in above, you need to return what are expecting to get from that method, for example, Rootobject .您不能像上面那样返回var ,您需要返回期望从该方法获得的内容,例如Rootobject

public async Task Main(string[] args)
{
    var result = await Api2();
    var myProVal = result.MyProperty;
}

public static async Task <Rootobject> Api2()
{
    var client = new RestClient("https://dummy.restapiexample2.com/api2/");
    var request = new RestRequest("Data");
    var response = await client.ExecuteAsync(request);
    if (response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        string rawResponse = response.Content;
        var root = JsonConvert.DeserializeObject<Rootobject>(rawResponse);
        return root;
    }
    else
        return null;
    }
}

And here you have been using async .在这里你一直在使用async The async keyword turns a method into an async method, which allows you to use the await keyword in its body. async 关键字将方法转换为异步方法,这样您就可以在其主体中使用 await 关键字。 When the await keyword is applied, it suspends the calling method and yields control back to its caller until the awaited task is complete.当应用 await 关键字时,它会挂起调用方法并将控制权交还给其调用者,直到等待的任务完成。 await can only be used inside an async method. await 只能在异步方法中使用。 if you don't have such a requirement don't use async, just make the method as synchronous .如果您没有这样的要求,请不要使用异步,只需将方法设置为synchronous即可。

Read more: https://learn.microsoft.com/en-us/do.net/csharp/async阅读更多: https://learn.microsoft.com/en-us/do.net/csharp/async

https://learn.microsoft.com/en-us/as.net/core/fundamentals/http-requests?view=as.netcore-7.0 https://learn.microsoft.com/en-us/as.net/core/fundamentals/http-requests?view=as.netcore-7.0

Updated:更新:

in the above code snippet, my Rootobject class as follows,在上面的代码片段中,我的Rootobject class 如下,

public class Rootobject
{
    public int MyProperty { get; set; }
    public string Name { get; set; }
    public string age { get; set; }
    public string addr { get; set; }
}

In the Main method, MyProperty is a sample property in that class. In your case it should be another one, it's up to you.在 Main 方法中, MyProperty是 class 中的示例属性。在您的情况下,它应该是另一个,这取决于您。 I think it's better if you can learn the basics before moving to this level.我认为如果你能在进入这个级别之前学习基础知识会更好。 good luck祝你好运

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

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