首页 文章

如何使用c#调用REST api?

提问于
浏览
246

这是我到目前为止的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Net.Http;
using System.Web;
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;
            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).

我正在使用Visual Studio 2011 Beta和.NET 4.5 Beta .

12 回答

  • 3

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

    我想我会发布一个更新的答案,因为大多数答案都是从2012年初开始的,这个帖子是Google搜索“call restful service c#”时的最佳结果之一 .

    Microsoft目前的指导是使用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语句的原因的更多详细信息,请参阅此问题及其答案:Do HttpClient and HttpClientHandler have to be disposed?

    有关更多详细信息,包括其他示例,请转到此处:http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client

    此博文可能也很有用:http://johnnycode.com/2012/02/23/consuming-your-own-asp-net-web-api-rest-service/

  • 1

    我的建议是使用RestSharp . 您可以调用REST服务并将它们转换为POCO对象,只需很少的样板代码即可实际解析响应 . 这不会解决您的特定错误,但会回答您关于如何调用REST服务的整体问题 . 必须更改您的代码才能使用它,这应该会在易用性和稳健性方面取得进展 . 这只是我的2美分

  • 0

    不相关,我敢肯定,但是要将 IDisposable 对象包装在 using 块中以确保正确处理:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System;
    using System.Web;
    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);
                }
    
            }
        }
    }
    
  • 339

    请使用以下代码来获取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;
                }
            }
        }
    }
    
  • -2

    Update for calling a REST API when using .NET 4.5 or .NET Core

    我建议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);
    
  • 1

    查看改装以从.net拨打休息服务 . 我发现它很容易使用:https://github.com/paulcbetts/refit

    改装:用于.NET Core,Xamarin和.NET Refit的自动类型安全REST库是一个受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");
    
  • 3

    GET:

    // 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;
        }
    }
    

    注意:为了序列化和绝望JSON,我使用了Newtonsoft.Json NuGet包 .

  • 9
    var TakingRequset = WebRequest.Create("http://xxx.acv.com/MethodName/Get");
        TakingRequset.Method = "POST";
        TakingRequset.ContentType = "text/xml;charset=utf-8";
        TakingRequset.PreAuthenticate = true;
    
        //---Serving Request path query
         var PAQ = TakingRequset.RequestUri.PathAndQuery;
    
        //---creating your xml as per the host reqirement
        string xmlroot=@"<root><childnodes>passing parameters</childnodes></root>";
        string xmlroot2=@"<root><childnodes>passing parameters</childnodes></root>";
    
        //---Adding Headers as requested by host 
        xmlroot2 = (xmlroot2 + "XXX---");
        //---Adding Headers Value as requested by host 
      //  var RequestheaderVales = Method(xmlroot2);
    
        WebProxy proxy = new WebProxy("XXXXX-----llll", 8080);
        proxy.Credentials = new NetworkCredential("XXX---uuuu", "XXX----", "XXXX----");
        System.Net.WebRequest.DefaultWebProxy = proxy;
    
    
        // Adding The Request into Headers
        TakingRequset.Headers.Add("xxx", "Any Request Variable ");
        TakingRequset.Headers.Add("xxx", "Any Request Variable");
    
        byte[] byteData = Encoding.UTF8.GetBytes(xmlroot);
        TakingRequset.ContentLength = byteData.Length;
    
        using (Stream postStream = TakingRequset.GetRequestStream())
        {
            postStream.Write(byteData, 0, byteData.Length);
            postStream.Close();
        }
    
    
    
        StreamReader stredr = new StreamReader(TakingRequset.GetResponse().GetResponseStream());
        string response = stredr.ReadToEnd();
    
  • 26

    这是一个可行的示例代码 . 我花了一天时间才能从Rest服务中读取一组对象:

    RootObject是从休息服务中读取的对象的类型 .

    string url = @"http://restcountries.eu/rest/v1";
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IEnumerable<RootObject>));
    WebClient syncClient = new WebClient();
    string content = syncClient.DownloadString(url);
    
    using (MemoryStream memo = new MemoryStream(Encoding.Unicode.GetBytes(content)))
    {
        IEnumerable<RootObject> countries = (IEnumerable<RootObject>)serializer.ReadObject(memo);    
    }
    
    Console.Read();
    
  • 14

    由于您使用的是Visual Studio 11 Beta,因此您需要使用最新且最好的 . 新的Web Api包含此类 .

    见HttpClient:http://wcf.codeplex.com/wikipage?title=WCF%20HTTP

  • 0

    我用这个简单的方式做了它,使用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 ;
    
  • 106

    第一步是为http客户端创建帮助程序类 .

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Threading.Tasks;
    
    namespace callApi.Helpers
    {
        public class CallApi
        {
            private readonly Uri BaseUrlUri;
            private HttpClient client = new HttpClient();
    
            public CallApi(string baseUrl)
            {
                BaseUrlUri = new Uri(baseUrl);
                client.BaseAddress = BaseUrlUri;
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
    
            }
    
            public HttpClient getClient()
            {
                return client;
            }
    
            public HttpClient getClientWithBearer(string token)
            {
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                return client;
            }
    
        }
    }
    

    然后,您可以在代码中使用此类 .

    这是一个如何使用上面的类调用没有持有者的其余api的示例 .

    // GET api/values
    [HttpGet]
    public async Task<ActionResult<string>> postNoBearerAsync(string email, string password,string baseUrl, string action)
    {
        var request = new LoginRequest
        {
            email = email,
            password = password
        };
    
        var callApi = new CallApi(baseUrl);
        var client = callApi.getClient();
        HttpResponseMessage response = await client.PostAsJsonAsync(action, request);
        if (response.IsSuccessStatusCode)
            return Ok(await response.Content.ReadAsAsync<string>());
        else
            return NotFound();
    }
    

    这是一个如何调用需要持有者的其余api的例子 .

    // GET api/values
    [HttpGet]
    public async Task<ActionResult<string>> getUseBearerAsync(string token, string baseUrl, string action)
    {
        var callApi = new CallApi(baseUrl);
        var client = callApi.getClient();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
        HttpResponseMessage response = await client.GetAsync(action);
        if (response.IsSuccessStatusCode)
        {
            return Ok(await response.Content.ReadAsStringAsync());
    
        }
        else
            return NotFound();
    }
    

    你还可以参考下面的repo,如果你想看到它的工作原理的工作实例 .

    https://github.com/mokh223/callApi

相关问题