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+"\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的更多相关文章
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- rabbitmq method之basic.consume
basic.consume指的是channel在 某个队列上注册消费者,那在这个队列有消息来了之后,就会把消息转发到给此channel处理,如果 这个队列有多个消费者,则会采用轮转的方式将消息分发给消 ...
- 中国Azure媒体服务RESTAPI的Endpoint
Amber Zhao Thu, Feb 26 2015 4:09 AM 由于海外Azure和中国Azure有不同的domain,很多用户在使用媒体服务RESTAPI时,需要指定中国Azure媒体服务 ...
- Flask框架搭建REST-API服务
一.目的 为了能够将测试工具部署成RESTful-API服务,这样就能通过接口的方式提供统一测试工具服务,使用人员就不用构建application而产生的各种环境问题.使用问题. 适合人群:Pytho ...
- [svc]简单理解什么是rpc调用?跟restapi有何区别?
什么是rpc调用 restapi调用方式是对数据的crud. 常见的我们写flash写个api,或者借助django drf写个标准的resetapi,一个url可以借助httpget post pu ...
- ComputeShader中Consume与AppendStructuredBuffer的使用
上个月写了一篇使用像素shader返回累加信息的Trick:https://www.cnblogs.com/hont/p/9977401.html 后来无意中发现DX11/Compute shader ...
- 调用kylin的restAPI接口构建cube
调用kylin的restAPI接口构建cube 参考:http://kylin.apache.org/docs/howto/howto_build_cube_with_restapi.html 1. ...
- geoserver源码学习与扩展——restAPI访问
产生这篇文章的想法是在前端通过js调用restAPI时,总是不成功,发送ajax请求时还总是出现类似跨域的问题,后来查找才发现,默认情况下restAPI的访问都需要管理员权限,而通过ajax请求传输用 ...
- ryu的RESTAPI简介——我主要用于下发和查看流表
一.Rest API简介 REST即表述性状态传递(RepreSentational State Transfer),是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性. 表 ...
随机推荐
- 多个线程运行MR程序时hadoop出现的问题
夜间多个任务同时并行,总有几个随机性有任务失败,查看日志: cat -n ads_channel.log |grep "Caused by" Caused by: java.uti ...
- pandas.apply()函数
1.介绍 apply函数是pandas里面所有函数中自由度最高的函数.该函数如下: DataFrame.apply(func, axis=0, broadcast=False, raw=False, ...
- netty源码解析(4.0)-27 ByteBuf内存池:PoolArena-PoolThreadCache
前面两章分析的PoolChunk和PoolSubpage,从功能上来说已经可以直接拿来用了.但直接使用这个两个类管理内存在高频分配/释放内存场景下会有性能问题,PoolChunk分配内存时算法复杂度最 ...
- 整理了8个Python中既冷门又实用的技巧
1.print 打印带有颜色的信息 大家知道 Python 中的信息打印函数 print,一般我们会使用它打印一些东西,作为一个简单调试. 但是你知道么,这个 Print 打印出来的字体颜色是可以设置 ...
- 详解Condition的await和signal等待/通知机制
本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...
- JS基础语法---Date对象---格式化日期
格式化后的指定格式的日期和时间,封装一个函数 function getDate() { var dt = new Date(); var year = dt.getFullYear(); var mo ...
- ABAP 字符串换行符处理
今天发现BW系统从K3系统数据库抽取会计科目的描述时,转换出错. 从监控器上看是值2KO 845 412D#有问题,但不知道有什么问题,检查后感觉是符号#的问题. 在转换中添加代码,替换#,发现没有起 ...
- 解决vue修改路由的查询字符串(query)url不改变,页面不刷新问题
我个人猜测可能是对路由的数据检测深度不够吧,单纯修改query里面的属性是不能触发数据驱动的,因此要直接给query赋值新的对象才能驱动数据更新,做法如下 第一种 var query=JSON.par ...
- Linux-3.14.12内存管理笔记【构建内存管理框架(2)】
前面构建内存管理框架,已经将内存管理node节点设置完毕,接下来将是管理区和页面管理的构建.此处代码实现主要在于setup_arch()下的一处钩子:x86_init.paging.pagetable ...
- Linux系统学习 四、网络基础—互联网概述,互联网接入方式
互联网概述 WWW:万维网 FTP:文件传输协议 E-MAIL:电子邮件 WWW 典型的C/S架构 URL:统一资源定位 协议+域名或IP:端口+网页路径+网页名 http://www.xxx.com ...