C# consume RestApi
2024-10-15 18:51:23
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);
}
}
}
最新文章
- BZOJ 2093: [Poi2010]Frog
- 个性化EDM数据营销的三大提醒
- 测试GeoGebra博客
- iOS App上架流程(2016详细版
- elecworks中“插入点”的意思
- RR模式下利用区间锁防止幻读,RC模式没有区间锁会出现幻读
- QR Code 码
- 快速构建Windows 8风格应用31-构建磁贴
- ajax 请求数据
- redis 中文存储乱码问题
- 前端请求参数MD5加密校验,参数串解密
- Python多线程爬虫与多种数据存储方式实现(Python爬虫实战2)
- Linux学习之CentOS(八)----文件与目录的默认权限与隐藏权限(转)
- java工具类(六)根据经纬度计算距离
- C语言字符串定义(数组&;指针)
- 腾讯的产品思维 VS 阿里的终局思维
- JavaScript——JS屏蔽F12和右键
- 【计算机网络】OSI七层模型图解
- python中ones的含义和用法
- MySQL学习(一) SQL基本操作
热门文章
- IT兄弟连 HTML5教程 HTML5文字版面和编辑标签 HTML框架结构
- numpy-np.ceil,np.floor,np.expand_dims方法
- Docker 系列之 常用镜像
- .net core 中间件使用
- Thinkphp 模板中 if 嵌套层级过多的问题,嵌套3级就报错,取消层级限制
- git submodule git 子模块管理相关操作
- raid组合优缺点介绍和创建LVM实验个人笔记
- Python语法速查: 4. 字符串常用操作
- Linux:VIM编辑器的使用
- A Neural Influence Diffusion Model for Social Recommendation 笔记