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+"\n");
} }
}
}

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

C# consume RestApi的更多相关文章

  1. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  2. rabbitmq method之basic.consume

    basic.consume指的是channel在 某个队列上注册消费者,那在这个队列有消息来了之后,就会把消息转发到给此channel处理,如果 这个队列有多个消费者,则会采用轮转的方式将消息分发给消 ...

  3. 中国Azure媒体服务RESTAPI的Endpoint

    Amber Zhao  Thu, Feb 26 2015 4:09 AM 由于海外Azure和中国Azure有不同的domain,很多用户在使用媒体服务RESTAPI时,需要指定中国Azure媒体服务 ...

  4. Flask框架搭建REST-API服务

    一.目的 为了能够将测试工具部署成RESTful-API服务,这样就能通过接口的方式提供统一测试工具服务,使用人员就不用构建application而产生的各种环境问题.使用问题. 适合人群:Pytho ...

  5. [svc]简单理解什么是rpc调用?跟restapi有何区别?

    什么是rpc调用 restapi调用方式是对数据的crud. 常见的我们写flash写个api,或者借助django drf写个标准的resetapi,一个url可以借助httpget post pu ...

  6. ComputeShader中Consume与AppendStructuredBuffer的使用

    上个月写了一篇使用像素shader返回累加信息的Trick:https://www.cnblogs.com/hont/p/9977401.html 后来无意中发现DX11/Compute shader ...

  7. 调用kylin的restAPI接口构建cube

    调用kylin的restAPI接口构建cube 参考:http://kylin.apache.org/docs/howto/howto_build_cube_with_restapi.html 1. ...

  8. geoserver源码学习与扩展——restAPI访问

    产生这篇文章的想法是在前端通过js调用restAPI时,总是不成功,发送ajax请求时还总是出现类似跨域的问题,后来查找才发现,默认情况下restAPI的访问都需要管理员权限,而通过ajax请求传输用 ...

  9. ryu的RESTAPI简介——我主要用于下发和查看流表

    一.Rest API简介 REST即表述性状态传递(RepreSentational State Transfer),是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性. 表 ...

随机推荐

  1. SQL Server 索引分析开关

    set statistics io onset statistics profile on

  2. 第04组 Beta冲刺(4/4)

    队名:斗地组 组长博客:地址 作业博客:Beta冲刺(4/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.分配展示任务 2.收集各个组员的进度 3.写博客 展示GitHub当日代码/文档 ...

  3. swoole是多进程还是多线程

    由于PHP语言不支持多线程,因此Swoole使用多进程模式.在多进程模式下存在进程内存隔离,在工作进程内修改global全局变量和超全局变量时,在其他进程是无效的. 进程隔离 $fds 虽然是全局变量 ...

  4. ModuleNotFoundError: No module named 'xxx'; 'xxx' is not a package

    错误: ModuleNotFoundError: No module named 'xxx'; 'xxx' is not a package 通过pycharm对脚本进行debug时,出现了如下错: ...

  5. 分享Java程序员50多道热门的多线程和并发面试题(答案解析)

    下面是Java程序员相关的热门面试题,你可以用它来好好准备面试. 1) 什么是线程? 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序员可以通过它进行多处理器 ...

  6. Java之ssh框架spring配置文件配置定时任务

    最近做了一个数据同步功能,要求晚上0点去定时同步数据,这是个老项目框架用的ssh,定时任务基于quartz,废话不多说,下面详细说说相关配置. 在spring的配置文件中: <!-- 0点定时任 ...

  7. IT兄弟连 HTML5教程 HTML5文字版面和编辑标签 HTML框架结构

    使用HTML框架结构可以把一个浏览器窗口划分为若干个小窗口,每个窗口可以显示不同的URL网页,每个框架里的网页相互独立.这样不仅可以非常方便地在浏览器中同时浏览不同的页面效果,而且可以非常方便地完成导 ...

  8. goweb- 对请求的处理

    对请求的处理 Go 语言的 net/http 包提供了一系列用于表示 HTTP 报文的结构,我们可以使用它 处理请求和发送相应,其中 Request 结构代表了客户端发送的请求报文,下面让我们看 一下 ...

  9. 本地SQL Server数据库提示网络问题无法连接

    运行程序时发现本地SQLserver数据库无法连接,提示信息为:在与SQL Server 建立连接时出现与网络相关的或特定与实例的错误.未能找到或无法访问服务器.请验证实例名称是否正确并且SQL Se ...

  10. SSH框架之Struts2第三篇

    1.3相关知识点 : 1.3.1 OGNL的表达式 : 1.3.1.1 什么是OGNL OGNL是Object-Graph Navigation Language的编写,它是一种功能强大的表达式语言, ...