如何使用C#调用REST API?

450

这是我目前的代码:

    public class Class1
    {
        private const string URL = "https://sub.domain.com/objects.json?api_key=123";
        private const string DATA = @"{""object"":{""name"":""Name""}}";

        static void Main(string[] args)
        {
            Class1.CreateObject();
        }

        private static void CreateObject()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.ContentLength = DATA.Length;
            StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
            requestWriter.Write(DATA);
            requestWriter.Close();

             try {
                WebResponse webResponse = request.GetResponse();
                Stream webStream = webResponse.GetResponseStream();
                StreamReader responseReader = new StreamReader(webStream);
                string response = responseReader.ReadToEnd();
                Console.Out.WriteLine(response);
                responseReader.Close();
            } catch (Exception e) {
                Console.Out.WriteLine("-----------------");
                Console.Out.WriteLine(e.Message);
            }

        }
    }

问题在于我认为异常块已被触发(因为当我删除try-catch时,会收到服务器错误(500)消息)。但我看不到我放在catch块中的Console.Out行。

我的控制台:

The thread 'vshost.NotifyLoad' (0x1a20) has exited with code 0 (0x0).
The thread '<No Name>' (0x1988) has exited with code 0 (0x0).
The thread 'vshost.LoadReference' (0x1710) has exited with code 0 (0x0).
'ConsoleApplication1.vshost.exe' (Managed (v4.0.30319)): Loaded 'c:\users\l. preston sego iii\documents\visual studio 11\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe', Symbols loaded.
'ConsoleApplication1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
A first chance exception of type 'System.Net.WebException' occurred in System.dll
The thread 'vshost.RunParkingWindow' (0x184c) has exited with code 0 (0x0).
The thread '<No Name>' (0x1810) has exited with code 0 (0x0).
The program '[2780] ConsoleApplication1.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[2780] ConsoleApplication1.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

此外,您是否已在其中设置了断点以查看它在哪里发生了问题? - NotMe
这是来自输出窗口而不是控制台的结果。 - Serj-Tm
5
MSDN有一篇关于构建RESTful服务的优秀文章:http://msdn.microsoft.com/library/dd203052.aspx ...以及关于构建RESTful客户端的文章:http://msdn.microsoft.com/en-us/magazine/ee309509.aspx。 - Lynn Crumbling
17个回答

537

ASP.NET Web API已经取代了之前提到的WCF Web API。

我想发布一个更新的答案,因为大多数回复都来自2012年初,当你在谷歌搜索"call restful service C#"时,这个帖子是排名靠前的结果之一。

微软目前的建议是使用Microsoft ASP.NET Web API客户端库来消费RESTful服务。这可以作为NuGet包Microsoft.AspNet.WebApi.Client获得。您需要将此NuGet包添加到您的解决方案中。

下面是使用ASP.NET Web API客户端库实现示例的方法:

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;

namespace ConsoleProgram
{
    public class DataObject
    {
        public string Name { get; set; }
    }

    public class Class1
    {
        private const string URL = "https://sub.domain.com/objects.json";
        private string urlParameters = "?api_key=123";

        static void Main(string[] args)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(URL);

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

            // List data response.
            HttpResponseMessage response = client.GetAsync(urlParameters).Result;  // Blocking call! Program will wait here until a response is received or a timeout occurs.
            if (response.IsSuccessStatusCode)
            {
                // Parse the response body.
                var dataObjects = response.Content.ReadAsAsync<IEnumerable<DataObject>>().Result;  //Make sure to add a reference to System.Net.Http.Formatting.dll
                foreach (var d in dataObjects)
                {
                    Console.WriteLine("{0}", d.Name);
                }
            }
            else
            {
                Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
            }

            // Make any other calls using HttpClient here.

            // Dispose once all HttpClient calls are complete. This is not necessary if the containing object will be disposed of; for example in this case the HttpClient instance will be disposed automatically when the application terminates so the following call is superfluous.
            client.Dispose();
        }
    }
}

如果您计划进行多个请求,应该重复使用您的HttpClient实例。有关为什么在这种情况下没有在HttpClient实例上使用using语句的更多详细信息,请参见此问题及其答案:HttpClient和HttpClientHandler在请求之间必须被处理吗? 有关其他示例,更多详细信息,请参见从.NET客户端(C#)调用Web API 这篇博客文章也可能会有所帮助:使用HttpClient来消耗ASP.NET Web API REST服务

12
谢谢!我需要安装WebApi客户端NuGet包才能让这个工作:Install-Package Microsoft.AspNet.WebApi.Client - Ev.
3
如果你需要模拟REST集成,即使使用客户端库也不容易。尝试使用RestSharp? - Rob Church
7
为了让这个答案变得更好,你应该将HttpClient声明包装在using语句中,以更好地管理你的资源 :) - Daniel Siebert
8
尝试使用ReadAsAsync(),但是遇到错误,无法使用。错误信息为“HttpContent不包含‘ReadAsAsync’的定义,也没有扩展方法。” - Robert Green MBA
8
иҰҒиҺ·еҸ–жү©еұ•ж–№жі•ReadAsAsync()пјҢиҜ·ж·»еҠ еҜ№ System.Net.Http.Formatting.dll зҡ„еј•з”ЁгҖӮпјҲеҫҲзӣҙи§ӮпјҢеҜ№еҗ§пјҹпјү - Arin
显示剩余10条评论

150

我建议使用RestSharp。你可以调用REST服务并将它们转换为POCO对象,而无需编写大量的样板代码来解析响应。这并不能解决你特定的错误,但它回答了你如何调用REST服务的总体问题。改变代码以使用它可能会在以后的易用性和鲁棒性方面更加有利。这只是我的个人看法。

示例:

namespace RestSharpThingy
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Reflection;
    using RestSharp;

    public static class Program
    {
        public static void Main()
        {
            Uri baseUrl = new Uri("https://httpbin.org/");
            IRestClient client = new RestClient(baseUrl);
            IRestRequest request = new RestRequest("get", Method.GET) { Credentials = new NetworkCredential("testUser", "P455w0rd") };

            request.AddHeader("Authorization", "Bearer qaPmk9Vw8o7r7UOiX-3b-8Z_6r3w0Iu2pecwJ3x7CngjPp2fN3c61Q_5VU3y0rc-vPpkTKuaOI2eRs3bMyA5ucKKzY1thMFoM0wjnReEYeMGyq3JfZ-OIko1if3NmIj79ZSpNotLL2734ts2jGBjw8-uUgKet7jQAaq-qf5aIDwzUo0bnGosEj_UkFxiJKXPPlF2L4iNJSlBqRYrhw08RK1SzB4tf18Airb80WVy1Kewx2NGq5zCC-SCzvJW-mlOtjIDBAQ5intqaRkwRaSyjJ_MagxJF_CLc4BNUYC3hC2ejQDoTE6HYMWMcg0mbyWghMFpOw3gqyfAGjr6LPJcIly__aJ5__iyt-BTkOnMpDAZLTjzx4qDHMPWeND-TlzKWXjVb5yMv5Q6Jg6UmETWbuxyTdvGTJFzanUg1HWzPr7gSs6GLEv9VDTMiC8a5sNcGyLcHBIJo8mErrZrIssHvbT8ZUPWtyJaujKvdgazqsrad9CO3iRsZWQJ3lpvdQwucCsyjoRVoj_mXYhz3JK3wfOjLff16Gy1NLbj4gmOhBBRb8rJnUXnP7rBHs00FAk59BIpKLIPIyMgYBApDCut8V55AgXtGs4MgFFiJKbuaKxq8cdMYEVBTzDJ-S1IR5d6eiTGusD5aFlUkAs9NV_nFw");
            request.AddParameter("clientId", 123);

            IRestResponse<RootObject> response = client.Execute<RootObject>(request);

            if (response.IsSuccessful)
            {
                response.Data.Write();
            }
            else
            {
                Console.WriteLine(response.ErrorMessage);
            }

            Console.WriteLine();

            string path = Assembly.GetExecutingAssembly().Location;
            string name = Path.GetFileName(path);

            request = new RestRequest("post", Method.POST);
            request.AddFile(name, File.ReadAllBytes(path), name, "application/octet-stream");
            response = client.Execute<RootObject>(request);
            if (response.IsSuccessful)
            {
                response.Data.Write();
            }
            else
            {
                Console.WriteLine(response.ErrorMessage);
            }

            Console.ReadLine();
        }

        private static void Write(this RootObject rootObject)
        {
            Console.WriteLine("clientId: " + rootObject.args.clientId);
            Console.WriteLine("Accept: " + rootObject.headers.Accept);
            Console.WriteLine("AcceptEncoding: " + rootObject.headers.AcceptEncoding);
            Console.WriteLine("AcceptLanguage: " + rootObject.headers.AcceptLanguage);
            Console.WriteLine("Authorization: " + rootObject.headers.Authorization);
            Console.WriteLine("Connection: " + rootObject.headers.Connection);
            Console.WriteLine("Dnt: " + rootObject.headers.Dnt);
            Console.WriteLine("Host: " + rootObject.headers.Host);
            Console.WriteLine("Origin: " + rootObject.headers.Origin);
            Console.WriteLine("Referer: " + rootObject.headers.Referer);
            Console.WriteLine("UserAgent: " + rootObject.headers.UserAgent);
            Console.WriteLine("origin: " + rootObject.origin);
            Console.WriteLine("url: " + rootObject.url);
            Console.WriteLine("data: " + rootObject.data);
            Console.WriteLine("files: ");
            foreach (KeyValuePair<string, string> kvp in rootObject.files ?? Enumerable.Empty<KeyValuePair<string, string>>())
            {
                Console.WriteLine("\t" + kvp.Key + ": " + kvp.Value);
            }
        }
    }

    public class Args
    {
        public string ClientId { get; set; }
    }

    public class Headers
    {
        public string Accept { get; set; }

        public string AcceptEncoding { get; set; }

        public string AcceptLanguage { get; set; }

        public string Authorization { get; set; }

        public string Connection { get; set; }

        public string Dnt { get; set; }

        public string Host { get; set; }

        public string Origin { get; set; }

        public string Referer { get; set; }

        public string UserAgent { get; set; }
    }

    public class RootObject
    {
        public Args args { get; set; }

        public Headers Headers { get; set; }

        public string Origin { get; set; }

        public string Url { get; set; }

        public string Data { get; set; }

        public Dictionary<string, string> Files { get; set; }
    }
}

6
RestSharp和JSON.NET绝对是不二之选。我发现微软工具集缺乏实用性且可能会失败。 - cbuteau
2
另外一位支持RestSharp的人,因为你可以更容易地模拟它进行测试,而不像WebApi客户端库那样困难。 - Rob Church
3
请提供一个例子来说明这个答案,谢谢。 - Caltor
1
RestSharp的默认JSON反序列化器不如Json.NET强大。 - user3791372
3
缺少示例使得这篇文章没有帮助! - smac2020
显示剩余5条评论

51

无关紧要,但请确保用using块包装您的IDisposable对象以确保正确释放:

using System;
using System.Net;
using System.IO;

namespace ConsoleProgram
{
    public class Class1
    {
        private const string URL = "https://sub.domain.com/objects.json?api_key=123";
        private const string DATA = @"{""object"":{""name"":""Name""}}";

        static void Main(string[] args)
        {
            Class1.CreateObject();
        }

        private static void CreateObject()
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.ContentLength = DATA.Length;
            using (Stream webStream = request.GetRequestStream())
            using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
            {
                requestWriter.Write(DATA);
            }

            try
            {
                WebResponse webResponse = request.GetResponse();
                using (Stream webStream = webResponse.GetResponseStream() ?? Stream.Null)
                using (StreamReader responseReader = new StreamReader(webStream))
                {
                    string response = responseReader.ReadToEnd();
                    Console.Out.WriteLine(response);
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("-----------------");
                Console.Out.WriteLine(e.Message);
            }
        }
    }
}

7
很好的答案,不使用常规 .NET 环境之外的任何额外包。 - palswim
@Jesse C. Slicer,为什么我在 WebResponse webResponse = request.GetResponse(); 中遇到了 404 错误? - Goh Han
3
为什么显示“资源未找到”?有很多,很多原因会导致404错误。 - Jesse C. Slicer
1
这是一个很棒的解决方案 @JesseC.Slicer。 我能够应用这段代码来获取令牌并从控制台中查看它。 您有任何提示可以帮助我使用此令牌进行身份验证/登录吗?我想使用GET方法来获取一些数据,但只有在已登录的情况下才能完成。我在哪里可以了解更多相关信息呢?谢谢! - Paul Laguna
使用 System 两次 :) - yossico
谢谢你的回答!如果有人在使用令牌,请在request.Method = "POST"之后添加request.Headers.Add("Authorization", "Bearer " + token); - dinozaver

36

以下是在C#中调用外部API的几种不同方法(更新于2019年)。

.NET的内置方式:

NuGet免费开源软件包,它们的开发人员体验比.NET内置客户端好得多:

  • ServiceStack.Text (1000个GitHub星,700万个NuGet下载) (*) - 快速,轻量级和弹性。
  • RestSharp (6000个GitHub星,2300万个NuGet下载) (*) - 简单的REST和HTTP API客户端
  • Flurl (1700个GitHub星,300万个NuGet下载) (*)- 流畅,可移植,可测试的HTTP客户端库

以上所有软件包都提供了出色的开发人员体验(即简洁,易于使用的API),并且得到了很好的维护。

(*) 截至2019年8月

示例:使用ServiceStack.Text从Fake Rest API获取待办事项。 其他库具有非常类似的语法。

class Program
{
    static void Main(string[] args)
    {
        // Fake rest API
        string url = "https://jsonplaceholder.typicode.com/todos/1";

        // GET data from API & map to POCO
        var todo =  url.GetJsonFromUrl().FromJson<Todo>();

        // Print the result to screen
        todo.PrintDump();
    }

    public class Todo
    {
        public int UserId { get; set; }
        public int Id { get; set; }
        public string Title { get; set; }
        public bool Completed { get; set; }
    }

}
在.NET Core控制台应用程序中运行上面的示例会产生以下输出。 Enter image description here 使用NuGet安装这些软件包。
Install-Package ServiceStack.Text, or

Install-Package RestSharp, or

Install-Package Flurl.Http

请记住,ServiceStack不是免费开源软件!免费版本的使用受到限制 - 详细信息请参见:https://servicestack.net/download#free-quotas - Tomasz
2
@Tomasz - ServiceStack.Text和上面展示的HttpUtils是免费、开源的,可以在https://github.com/ServiceStack/ServiceStack.Text获取。 - Mark
1
是的,你说得对,其中的ServiceStack.Text部分是FOSS,感谢你纠正我。 - Tomasz
使用 IHttpClientFactory 和 GetFromJsonAsync? - Kiquenet

26

ASP.NET Core 中的解决方案:

using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace WebApp
{
    public static class HttpHelper
    {
        // In my case this is https://localhost:44366/
        private static readonly string apiBasicUri = ConfigurationManager.AppSettings["apiBasicUri"];

        public static async Task Post<T>(string url, T contentValue)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(apiBasicUri);
                var content = new StringContent(JsonConvert.SerializeObject(contentValue), Encoding.UTF8, "application/json");
                var result = await client.PostAsync(url, content);
                result.EnsureSuccessStatusCode();
            }
        }

        public static async Task Put<T>(string url, T stringValue)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(apiBasicUri);
                var content = new StringContent(JsonConvert.SerializeObject(stringValue), Encoding.UTF8, "application/json");
                var result = await client.PutAsync(url, content);
                result.EnsureSuccessStatusCode();
            }
        }

        public static async Task<T> Get<T>(string url)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(apiBasicUri);
                var result = await client.GetAsync(url);
                result.EnsureSuccessStatusCode();
                string resultContentString = await result.Content.ReadAsStringAsync();
                T resultContent = JsonConvert.DeserializeObject<T>(resultContentString);
                return resultContent;
            }
        }

        public static async Task Delete(string url)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(apiBasicUri);
                var result = await client.DeleteAsync(url);
                result.EnsureSuccessStatusCode();
            }
        }
    }
}

要发布内容,请使用类似以下的方式:

await HttpHelper.Post<Setting>($"/api/values/{id}", setting);

删除示例:

await HttpHelper.Delete($"/api/values/{id}");

获取列表的示例:

List<ClaimTerm> claimTerms = await HttpHelper.Get<List<ClaimTerm>>("/api/values/");

获取单个示例:

ClaimTerm processedClaimImage = await HttpHelper.Get<ClaimTerm>($"/api/values/{id}");

3
这是一段非常好的代码,但你不应该在using块内使用httpclient。请参阅https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/。 - Myke Black
你最好使用这个代码块,而不是"result.EnsureSuccessStatusCode();"。if (result.IsSuccessStatusCode) { // 处理成功 } else { // 处理失败 } - Project Mayhem
你表达得非常清晰。太好了! - Blasco73

21

请使用以下代码进行REST API请求:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Json;

namespace ConsoleApplication2
{
    class Program
    {
        private const string URL = "https://XXXX/rest/api/2/component";
        private const string DATA = @"{
            ""name"": ""Component 2"",
            ""description"": ""This is a JIRA component"",
            ""leadUserName"": ""xx"",
            ""assigneeType"": ""PROJECT_LEAD"",
            ""isAssigneeTypeValid"": false,
            ""project"": ""TP""}";

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

        private static void AddComponent()
        {
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            client.BaseAddress = new System.Uri(URL);
            byte[] cred = UTF8Encoding.UTF8.GetBytes("username:password");
            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            System.Net.Http.HttpContent content = new StringContent(DATA, UTF8Encoding.UTF8, "application/json");
            HttpResponseMessage messge = client.PostAsync(URL, content).Result;
            string description = string.Empty;
            if (messge.IsSuccessStatusCode)
            {
                string result = messge.Content.ReadAsStringAsync().Result;
                description = result;
            }
        }
    }
}

1
-1: .NET是一个托管平台,但HttpClient是非托管的(这意味着你必须使用using来告诉它何时可以处理那些非托管指针)。如果没有它,你的代码将无法扩展到几个用户(而且,是的,这很重要,重要到语言都有一个特定的关键字来处理它)。 - JCKödel
5
@JCKödel - 您在这里并不完全正确,应该阅读这篇文章 https://dev59.com/xGEh5IYBdhLWcg3wTB_2#22561368 - HttpClient被设计为可用于多次调用。 - hB0
1
是的@JCKödel,请阅读这篇文章 https://dev59.com/WGUo5IYBdhLWcg3w3ymi - Nathan
1
我认为@JCKödel是完全正确的!在上面发布的代码中,HttpClient被一遍又一遍地实例化,这是不正确的。请注意以下说明:“HttpClient旨在在应用程序的整个生命周期内只实例化一次并重复使用。在每个请求中实例化HttpClient类将在重负载下耗尽可用的套接字数量。这将导致SocketException错误。下面是一个正确使用HttpClient的示例。”来自Microsoft Docs - Tomasz

11

.NET 4.5或.NET Core中调用REST API

我建议使用DalSoft.RestClient(注:我创建了它)。原因是,它使用动态类型,您可以将所有内容封装在一个流畅的调用中,包括序列化/反序列化。下面是一个可工作的PUT示例:

dynamic client = new RestClient("http://jsonplaceholder.typicode.com");

var post = new Post { title = "foo", body = "bar", userId = 10 };

var result = await client.Posts(1).Put(post);

7

获取:

// GET JSON Response
public WeatherResponseModel GET(string url) {
    WeatherResponseModel model = new WeatherResponseModel();
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    try {
        WebResponse response = request.GetResponse();
        using(Stream responseStream = response.GetResponseStream()) {
            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
            model = JsonConvert.DeserializeObject < WeatherResponseModel > (reader.ReadToEnd());
        }
    } catch (WebException ex) {
        WebResponse errorResponse = ex.Response;
        using(Stream responseStream = errorResponse.GetResponseStream()) {
            StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
            String errorText = reader.ReadToEnd();
            // Log errorText
        }
        throw;
    }
    return model;
}

POST:

// POST a JSON string
void POST(string url, string jsonContent) {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = "POST";

    System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
    Byte[]byteArray = encoding.GetBytes(jsonContent);

    request.ContentLength = byteArray.Length;
    request.ContentType =  @ "application/json";

    using(Stream dataStream = request.GetRequestStream()) {
        dataStream.Write(byteArray, 0, byteArray.Length);
    }

    long length = 0;
    try {
        using(HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
            // Got response
            length = response.ContentLength;
        }
    } catch (WebException ex) {
        WebResponse errorResponse = ex.Response;
        using(Stream responseStream = errorResponse.GetResponseStream()) {
            StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
            String errorText = reader.ReadToEnd();
            // Log errorText
        }
        throw;
    }
}

注意:我使用了Newtonsoft.Json NuGet包来进行JSON的序列化和反序列化。

6

请查看Refit,用于从.NET调用REST服务。我发现它非常易于使用:

Refit: 自动类型安全的.NET Core、Xamarin和.NET REST库

Refit是一个受Square的Retrofit库启发的库,它可以将您的REST API转换为实时接口:

public interface IGitHubApi {
        [Get("/users/{user}")]
        Task<User> GetUser(string user);
}

// The RestService class generates an implementation of IGitHubApi
// that uses HttpClient to make its calls:

var gitHubApi = RestService.For<IGitHubApi>("https://api.github.com");

var octocat = await gitHubApi.GetUser("octocat");

你知道 Refit 是否使用反射来实现这个吗?我找不到任何信息。 - tfrascaroli
抱歉 @tfrascaroli,我手头不确定。 - patrickbadley

2

我用Web API 2.0以这种简单的方式完成了它。您可以删除UseDefaultCredentials。我将其用于我的个人用例。

List<YourObject> listObjects = new List<YourObject>();

string response = "";
using (var client = new WebClient() { UseDefaultCredentials = true })
{
     response = client.DownloadString(apiUrl);
}

listObjects = JsonConvert.DeserializeObject<List<YourObject>>(response);
return listObjects;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接