zoukankan      html  css  js  c++  java
  • C# consume RestApi

    1.RestSharp.

    Nuget install RestSharp,Newtonsoft.Json.

    using System;
    using RestSharp;
    using Newtonsoft.Json.Linq;
    
    namespace DBDll
    {
        public class RestSharpApi
        {
            public static void GetWebResonse(string baseUrl = "https://api.github.com/repos/restsharp/restsharp/releases")
            {            
                var client = new RestClient(baseUrl);
                IRestResponse response = client.Execute(new RestRequest());
                //return the formatted json string from a clumsy json string.
                var releases = JArray.Parse(response.Content);
                Console.WriteLine(releases);
            }
        }
    }

    2.HttpWebRequest

    using Newtonsoft.Json.Linq;
    using System;
    using System.IO;
    using System.Net;
    
    namespace DBDll
    {
        public class HttpWebRequestDemo
        {
            public static void HttpWebRequestShow(string baseUrl = "https://api.github.com/repos/restsharp/restsharp/releases")
            {
                var httpRequest = (HttpWebRequest)WebRequest.Create(baseUrl);
                httpRequest.Method = "GET";
                httpRequest.UserAgent = "Mozilla / 5.0(Windows NT 6.1; Win64; x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 58.0.3029.110 Safari / 537.36";
                httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                string content = string.Empty;
                using(var responseStream=httpResponse.GetResponseStream())
                {
                    using(var sr=new StreamReader(responseStream))
                    {
                        content = sr.ReadToEnd();
                    }
                }
                Console.WriteLine(content);
            }
        }
    }

    3.HttpClient

    using System;
    using System.IO;
    using System.Net.Http;
    
    namespace DBDll
    {
        public class HttpClientDemo
        {
            public static void HttpClientShow(string url)
            {
                HttpClient httpClient = new HttpClient();
                var response = httpClient.GetStringAsync(url);            
                Console.WriteLine(response.Result);
                string textFile = Directory.GetCurrentDirectory() + "//" + "web.txt";
                using(StreamWriter webWriter=new StreamWriter(textFile,true))
                {
                    webWriter.WriteLine(response.Result+"
    ");
                }
                
            }        
        }
    }

     4.ServiceStack.

    Install ServiceStack in Nuget.

    using System;
    using ServiceStack;
    
    namespace DBDll
    {
        public class ServiceStackDemo
        {
            public static void ServiceStackShow(string url)
            {
                var response= url.GetJsonFromUrl();
                Console.WriteLine(response);
            }
        }
    }
  • 相关阅读:
    java开发中遇到的问题及解决方法(持续更新)
    java.lang.UnsatisfiedLinkError: no XXX in java.library.path
    JNI与JNA性能比较
    JNA—JNI终结者
    JNI的替代者—使用JNA访问Java外部功能接口
    iOS开发UI篇—多控制器和导航控制器简单介绍
    李洪强iOS开发-网络新闻获取数据思路回顾
    iOS开发UI篇—Date Picker和UITool Bar控件简单介绍
    iOS开发UI篇—程序启动原理和UIApplication
    iOS开发UI篇—常见的项目文件介绍
  • 原文地址:https://www.cnblogs.com/Fred1987/p/11434256.html
Copyright © 2011-2022 走看看