当前位置:网站首页>C # use restsharp library to realize post request
C # use restsharp library to realize post request
2022-07-29 09:10:00 【flysh05】
Last temporary use Python send out POST request , Get the required information from the specified website . I thought Python Handy , The supported libraries are also familiar , Just use 3 Minutes will this POST The requested function is written , Also packed into exe file , stay C# Application calls , And can normally obtain the required information , The problem is solved , But I received many enthusiastic friends , Put forward a question ,C# It can be realized by itself POST request , There are also many practical libraries that can be used , Why do you toss about so much !
In daily work and life , I think the first thing is to solve the problem , No matter what way I use , First of all, implement the required functions , The second is to pursue efficiency and the complexity of solving problems . today , Used RestSharp The library tested , It's OK, too . However, this library has made important adjustments , Some changes have taken place in the way of use .
https://restsharp.dev/v107/#mocking
RestSharp The request body can be serialized into JSON or XML And deserialize the response . It can also be based on different parameter types ( route 、 Inquire about 、 Form or body ) Form a valid request URI.RestSharp The latest version of is v107. This is a major upgrade , It contains quite a few major changes . The most important change is RestSharp Stop using legacy HttpWebRequest class , Instead, use the well-known “HttpClient”. This measure has solved many problems , For example, due to incorrect HttpClient The instance cache causes the connection to hang 、 Updated protocol support and many other issues . Another major change is SimpleJson Retire completely from the code base . contrary ,RestSharp Use JsonSerializer In bag System.Text.Json, This is a ASP.NET Core Default serializer for .
Installation Library RestSharp.108.0.1
encapsulation GET and POST function
/// <summary>
/// Post Submit
/// </summary>
/// <param name="url"> Submit the address </param>
/// <param name="content"> The submitted Post Information </param>
/// <param name="contentType"> Submission information format type </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 Submit
/// </summary>
/// <param name="url"> Submit the address </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();
}
}
test OK, And call Python The result achieved by our application is the same !
Detailed documents can be viewed on the official website , I only use it to realize my own needs .
边栏推荐
- BI data analysis practitioners learn financial knowledge from scratch? What introductory books are recommended
- Opencv cvcircle function
- Could not receive a message from the daemon
- 2022 question bank and answers of operation certificate examination for main principals of hazardous chemical business units
- Application of matrix transpose
- STM32 application development practice tutorial: design and implementation of controllable LED water lamp
- 2022年R2移动式压力容器充装考题模拟考试平台操作
- Collation of ml.net related resources
- Regular expression verification version number
- Asp graduation project - based on C # +asp Design and implementation of enterprise investment value analysis system based on. Net + sqlserver (graduation thesis + program source code) -- enterprise in
猜你喜欢
2022年山东省安全员C证上岗证题库及答案
Flowable UI production flow chart
Introduction to translation professional qualification (level) examination
数学建模——聚类
Asp graduation project - based on C # +asp Design and implementation of enterprise investment value analysis system based on. Net + sqlserver (graduation thesis + program source code) -- enterprise in
Quaternion and its simple application in unity
Jetpack Glance? The spring of widgets is coming
Sword finger offer 32 - ii Print binary tree II from top to bottom
C language -- 22 one dimensional array
(Video + graphic) introduction to machine learning series - Chapter 3 logical regression
随机推荐
How to choose effective keywords
Want to know how to open an account through online stock? Excuse me, is it safe to open a stock account by mobile phone?
What should I pay attention to now? Excuse me, is it safe to open a stock account by mobile phone?
数学建模——聚类
Sword finger offer 32 - ii Print binary tree II from top to bottom
Acwing game 59 [End]
Use disco diffusion to generate AI artwork in moment pool cloud
STM32 application development practice tutorial: design and implementation of controllable LED water lamp
Emmet syntax
Database system design: partition
01 knapsack about from two-dimensional optimization to one-dimensional optimization
英语高频后缀
Jetpack Glance? The spring of widgets is coming
ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
Asp graduation project - based on C # +asp Design and implementation of enterprise investment value analysis system based on. Net + sqlserver (graduation thesis + program source code) -- enterprise in
mysql怎么换成中文
201803-3 CCF URL映射 满分题解
Asp graduation project - based on C # +asp Net+sqlserver laboratory reservation system design and Implementation (graduation thesis + program source code) - Laboratory Reservation System
完全背包问题 从朴素到终极
Sword finger offer 50. the first character that appears only once