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),是一种针对网络应用的设计和开发方式,可以降低开发的复杂性,提高系统的可伸缩性. 表 ...
随机推荐
- shell中字典的一个用法示例
1. shell中字典的用法示例, 数组用法同字典 2. 以上使用sell配合字典实现的功能, 使用awk能轻松搞定, 如下: awk '{print $2}' file.txt | sort | u ...
- python 读取ini 配置文件
安装 pip install configparser 1 配置文件 config.ini: [MysqlDB]user=rootpasswd=123456sport=3306db_name=my_d ...
- 普通的maven项目,如何打成一个fat jar(包括了全部依赖jar包)?
1.前言 用过spring boot的同学肯定知道,现在web项目可以直接打成jar包运行,相当方便. 那么普通项目如何配置(非spring boot),才能打成一个类似的jar包呢? 2.解决方案: ...
- Spring Boot可执行Jar包运行原理
目录 1. 打可执行Jar包 2. 可执行Jar包内部结构 3. JarLauncher 4. 简单总结 5. 远程调试 Spring Boot有一个很方便的功能就是可以将应用打成可执行的Jar.那么 ...
- [Spring cloud 一步步实现广告系统] 9. 主类和配置文件
搜索系统启动主类 /** * AdSearchApplication for 广告搜索服务启动类 * * @author <a href="mailto:magicianisaac@g ...
- Java每日一面(Part1:计算机网络)[19/10/14]
作者:故事我忘了¢个人微信公众号:程序猿的月光宝盒 1.1 说一说TCP的四次挥手 "挥手",即终止TCP连接,断开一个TCP连接池. 需要客户端和服务端总共发出四个包,以 ...
- js-事件函数调用简化
// 一般写法 function fn(event) { console.log(event) } div.onclick = function (event) { fn(event) } ===== ...
- Cesium专栏-裁剪效果(基于3dtiles模型,附源码下载)
Cesium Cesium 是一款面向三维地球和地图的,世界级的JavaScript开源产品.它提供了基于JavaScript语言的开发包,方便用户快速搭建一款零插件的虚拟地球Web应用,并在性能,精 ...
- 激活windows操作系统的命令行指令
1.开始菜单中搜索命令提示符,右键管理员运行 2.为了避免激活的失败,首先卸载已经过期的密钥 输入以下命令:slmgr.vbs /upk 回车确认之后会弹出提示“已成功卸载了产品密钥” 3.下一步是将 ...
- Python 3 线程模型,进程模型记录
最近需要使用 python3 多线程处理大型数据,顺道探究了一下,python3 的线程模型的情况,下面进行简要记录: 多线程运行的优点: 使用线程可以把程序中占用时间较长的任务放到后台去处理: 用户 ...