当前位置:网站首页>C# 使用RestSharp库实现POST请求
C# 使用RestSharp库实现POST请求
2022-07-29 09:02:00 【flysh05】
上次临时使用Python发送POST请求,从指定网址获取需要的信息。当时觉得Python顺手,支持的库也熟悉些,就使用3分钟将这个POST请求的功能写好了,也打包成为了exe文件,在C#应用程序调用,且能正常获取需要的信息,问题是解决了,但是收到了许多热心的朋友,提出了疑问,C#本身就可以实现POST请求,也又很多实用的库可以使用,为什么这么折腾啊!
在日常工作和生活中,我觉得首要的是解决问题,不管我用什么方式,首先的实现需要的功能,其次才追寻效率和解决问题的复杂性。今天,使用了RestSharp库测试了一下,也是可以的。不过这个库做了重要的调整,使用方式发生了一些改变。
https://restsharp.dev/v107/#mocking
RestSharp 可以将请求正文序列化为 JSON 或 XML 并反序列化响应。它还可以根据不同的参数类型(路径、查询、表单或正文)形成有效的请求 URI。RestSharp 的最新版本是 v107。这是一次重大升级,其中包含相当多的重大更改。最重要的变化是 RestSharp 停止使用遗留HttpWebRequest类,而是使用众所周知的“HttpClient”。这一举措解决了很多问题,例如由于不正确的HttpClient实例缓存导致连接挂起、更新的协议支持以及许多其他问题。另一个重大变化是SimpleJson完全从代码库中退休。相反,RestSharp 使用JsonSerializer包中的System.Text.Json,这是 ASP.NET Core 的默认序列化程序。
安装库 RestSharp.108.0.1
封装GET和POST函数
/// <summary>
/// Post提交
/// </summary>
/// <param name="url">提交地址</param>
/// <param name="content">提交的Post信息</param>
/// <param name="contentType">提交信息格式类型</param>
/// <returns></returns>
public static string HttpPost(string url, string content, string contentType)
{
try
{
var client = new RestClient(url);
var request = new RestRequest();
request.Method = Method.Post;
request.Timeout = 5000;
request.AddParameter(contentType, content, ParameterType.RequestBody);
var response = client.Execute(request);
return response.Content;
}
catch (Exception ex)
{
return ex.ToString();
}
}
/// <summary>
/// Get提交
/// </summary>
/// <param name="url">提交地址</param>
/// <returns></returns>
public static string HttpGet(string url)
{
try
{
var client = new RestClient(url);
var request = new RestRequest();
request.Method = Method.Get;
request.Timeout = 5000;
request.AddHeader("content-type", "text/html; charset=utf-8");
request.AddHeader("content-encoding", "gzip");
var response = client.Execute(request);
return response.Content;
}
catch (Exception ex)
{
return ex.ToString();
}
}
测试OK,和调用Python的应用程序达到的结果一样!
详细文档可去官网查看,我仅仅用来实现自己的需求。
边栏推荐
- 正则表达式校验版本号
- Complete knapsack problem from simplicity to ultimate
- MySQL statement mind map
- Classic interview question: = = the difference between equals
- Source code compilation pytorch pit
- access数据库可以被远程访问吗
- 201803-3 CCF URL映射 满分题解
- ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
- Compile and install Apache for rocky Foundation
- 01背包关于从二维优化到一维
猜你喜欢

2022 Teddy cup data mining challenge C project and post game summary

The use and Simulation of string function, character function and memory function

7.3-function-templates

Count the list of third-party components of an open source project

C language watch second kill assist repeatedly

Sword finger offer 32 - ii Print binary tree II from top to bottom
Jetpack Glance? The spring of widgets is coming

LeetCode刷题(6)

Could not receive a message from the daemon

Summary of some experiences in the process of R & D platform splitting
随机推荐
Introduction to translation professional qualification (level) examination
Error reporting when adding fields to sap se11 transparent table: structural changes at the field level (conversion table xxxxx)
Database system design: partition
Leetcode deduction topic summary (topic No.: 53, 3, 141, interview question 022, the entry node of the link in the sword finger offer chain, 20, 19, Niuke NC1, 103, 1143, Niuke 127)
Gutcloud technology restcloud completed the pre-A round of financing of tens of millions of yuan
CVPR 2022 | clonedperson: building a large-scale virtual pedestrian data set of real wear and wear from a single photo
Could not receive a message from the daemon
Group Backpack
英语高频后缀
Squareline partners with visual GUI development of oneos graphical components
(Video + graphic) introduction series to machine learning - Chapter 2 linear regression
How to choose effective keywords
English high frequency suffix
Flowable 基础篇2
6.3 references
Mathematical modeling - Differential Equations
【Unity入门计划】C#与Unity-了解类和对象
(视频+图文)机器学习入门系列-第2章 线性回归
Summary of some experiences in the process of R & D platform splitting
Arfoundation starts from scratch 5-ar image tracking