C# 从C调用RESTAPI#

C# 从C调用RESTAPI#,c#,rest,C#,Rest,我有一个用JAVA开发的基于REST的API。现在我尝试从基于控制台的C#应用程序(即从它的主函数)调用该API。我想知道有没有可能做到这一点。 我试过一些东西,但不起作用 //我已经在我的类文件中编写了以下代码。但是我找不到RestClient类。我需要包括什么 static void Main(string[] args) { { string endPoint = @"http:\\myRestService.c

我有一个用JAVA开发的基于REST的API。现在我尝试从基于控制台的C#应用程序(即从它的主函数)调用该API。我想知道有没有可能做到这一点。 我试过一些东西,但不起作用

//我已经在我的类文件中编写了以下代码。但是我找不到RestClient类。我需要包括什么

static void Main(string[] args)
        {
             {

                 string endPoint = @"http:\\myRestService.com\api\";
                 var client = new RestClient(endPoint);
                 var json = client.MakeRequest();
              }
         }
从asp.net网站上的。这显示了它是如何在C#中完成的,您尝试使用的RestClient是一个库,它封装了类似于此示例的内容。RestClient可以作为nugget包添加

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace ProductStoreClient
{
    class Product
    {
        public string Name { get; set; }
        public double Price { get; set; }
        public string Category { get; set; }
    }

    class Program
    {
        static void Main()
        {
            RunAsync().Wait();
        }

        static async Task RunAsync()
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:9000/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                // HTTP GET
                HttpResponseMessage response = await client.GetAsync("api/products/1");
                if (response.IsSuccessStatusCode)
                {
                    Product product = await response.Content.ReadAsAsync<Product>();
                    Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
                }

                // HTTP POST
                var gizmo = new Product() { Name = "Gizmo", Price = 100, Category = "Widget" };
                response = await client.PostAsJsonAsync("api/products", gizmo);
                if (response.IsSuccessStatusCode)
                {
                    Uri gizmoUrl = response.Headers.Location;

                    // HTTP PUT
                    gizmo.Price = 80;   // Update price
                    response = await client.PutAsJsonAsync(gizmoUrl, gizmo);

                    // HTTP DELETE
                    response = await client.DeleteAsync(gizmoUrl);
                }
            }
        }
    }
}
使用系统;
使用System.Net.Http;
使用System.Net.Http.Header;
使用System.Threading.Tasks;
命名空间ProductStoreClient
{
类产品
{
公共字符串名称{get;set;}
公共双价{get;set;}
公共字符串类别{get;set;}
}
班级计划
{
静态void Main()
{
RunAsync().Wait();
}
静态异步任务RunAsync()
{
使用(var client=new HttpClient())
{
client.BaseAddress=新Uri(“http://localhost:9000/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
//HTTP获取
httpresponsemessageresponse=wait client.GetAsync(“api/products/1”);
if(响应。IsSuccessStatusCode)
{
Product=wait response.Content.ReadAsAsync();
Console.WriteLine(“{0}\t${1}\t{2}”,product.Name,product.Price,product.Category);
}
//HTTP POST
var gizmo=new Product(){Name=“gizmo”,Price=100,Category=“Widget”};
response=wait client.postsjsonasync(“api/产品”,gizmo);
if(响应。IsSuccessStatusCode)
{
Uri gizmoUrl=response.Headers.Location;
//HTTP PUT
gizmo.Price=80;//更新价格
response=wait client.PutAsJsonAsync(gizmoUrl,gizmo);
//HTTP删除
response=wait client.deleteAync(gizmoUrl);
}
}
}
}
}

什么是
RestClient
?我不太清楚C,但你需要启动一个新的JVM来运行java代码。像
java-jarmyapplication.jar
,如果这个应用程序能够接收您的URL作为参数,那么一切都可以。