简体   繁体   English

如何从.NET调用Node.js REST API

[英]How to call node.js REST API from .NET

If I put the jquery code below within the script tag within a html page and drag the html page into a web browser the call to the API specified in the URL is made and I get back a response in JSON format. 如果我将jQuery代码放在html页面内的script标记内,然后将html页面拖到Web浏览器中,则将调用URL中指定的API,并以JSON格式返回响应。 So this works good. 所以这很好。

The reason I want to use .NET for calling the rest API that is made in node.js is because I want to use the unit test utility that exist in visual studio. 我之所以想使用.NET来调用node.js中创建的其余API,是因为我想使用Visual Studio中存在的单元测试实用程序。

So when I start the unit test the call to the REST API made in node.js should be made and then I can check whatever I want in the returned json format by using the assert.AreEqual. 因此,当我开始单元测试时,应该对node.js中的REST API进行调用,然后可以使用assert.AreEqual检查返回的json格式的内容。

I have googled a lot and there is several example about Unit Testing Controllers in ASP.NET Web API 2 but I don't want to unit test controller. 我已经在Google上搜索了很多,并且在ASP.NET Web API 2中有几个关于单元测试控制器的示例,但是我不想对单元测试控制器进行单元测试。 I only want to call the REST API(made in node.js) when I start my unit test. 当我开始单元测试时,我只想调用REST API(由node.js制造)。

I assume to use .NET in the way I want is probably quite rare. 我假设以我想要的方式使用.NET可能很少。

If it's not possible to use .NET and unit test in the way that I want here I will use another test framework. 如果无法以我想要的方式使用.NET和单元测试,我将使用另一个测试框架。 I hope to get some help from here. 我希望从这里得到一些帮助。

Hope you understand what I mean. 希望你明白我的意思。

$.ajax({
    type: 'GET',
    url: 'http://10.1.23.168:3000/api/v1/users/1',
    dataType: 'json',
    async: false,
    headers: {
    'Authorization': 'Basic ' + btoa('DEFAULT/user:password')
    },
    success: function(response) {
        //your success code
        console.log(response);
    },
    error: function (err) {
        //your error code
        console.log(err);   
    }   
});

Many thanks 非常感谢

Basically what you need to do is to call node.js' API from your C# test code in a same way you call it using jQuery. 基本上,您需要做的就是以与使用jQuery调用它相同的方式,从C#测试代码中调用node.js的API。 There are several ways to do it: 有几种方法可以做到这一点:

  1. Use HttpWebRequest class https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=vs.110%29.aspx 使用HttpWebRequest类https://msdn.microsoft.com/zh-cn/library/system.net.httpwebrequest%28v=vs.110%29.aspx
  2. Use HttpClient class https://msdn.microsoft.com/en-us/library/system.net.http.httpclient%28v=vs.118%29.aspx It's more "RESTable" since it exposes methods to call HTTP methods like GET, PUT, POST and DELETE methods directly. 使用HttpClient类https://msdn.microsoft.com/zh-cn/library/system.net.http.httpclient%28v=vs.118%29.aspx由于它公开了诸如HTTP之类的调用方法,因此它更“可REST”直接使用GET,PUT,POST和DELETE方法。
  3. 3rd party software http://restsharp.org/ 第三方软件http://restsharp.org/

Generally I recommend approach #2. 通常,我建议使用方法2。

Here's the example source with all the rest of the code. 这是带有所有其余代码的示例源。

Another resource is the docs. 另一个资源是文档。

This code snippet should be enough to get you where you need. 此代码段应足以使您到达所需的位置。

using(var client = newHttpClient())  
{  
    client.BaseAddress = newUri("http://localhost:55587/");  
    client.DefaultRequestHeaders.Accept.Clear();  
    client.DefaultRequestHeaders.Accept.Add(newMediaTypeWithQualityHeaderValue("application/json"));  
    //GET Method  
    HttpResponseMessage response = awaitclient.GetAsync("api/Department/1");  
    if (response.IsSuccessStatusCode)  
    {  
        Departmentdepartment = awaitresponse.Content.ReadAsAsync < Department > ();  
        Console.WriteLine("Id:{0}\tName:{1}", department.DepartmentId, department.DepartmentName);  
        Console.WriteLine("No of Employee in Department: {0}", department.Employees.Count);  
    }  
    else  
    {  
        Console.WriteLine("Internal server Error");  
    }  
}

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

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