当前位置:网站首页>C# uses RestSharp to implement Get, Post requests (2)
C# uses RestSharp to implement Get, Post requests (2)
2022-07-30 08:09:00 【flysh05】
Tested the library again today,的确很好用,Originally used for conveniencePython应用,go around some.
See how the network actually uses the request method,如下图:

封装一个POST请求的方法:
static string PostBySap(string sap)
{
string url = "http://192.168.0.222:7890/UIHandler/ItemQueryHandle.ashx";
using (var client = new RestClient(url))
{
string data = $"action=query&ItemCode={
sap}&Plant=&Desc\_CH=&Item\_Group=&page=1&rows=30";
var request = new RestRequest();
request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
request.AddHeader("Referer", "http://192.168.0.122:3311/ItemQuery.aspx");
request.AddStringBody(data, DataFormat.None);
var response = client.PostAsync(request);
var result = response.Result.Content;
return result;
}
}
Pass in a variable data,返回响应的内容,测试OK.
封装一个Get请求:
static void GetBySAP(string sap, Language language)
{
string url = "http://192.168.0.234:8011/api/Item/Gets?";
string data = "";
switch (language)
{
case Language.Chinese:
data = $"Item\_Code\_SAP={
sap}&&Plant\_SAP=6000&EN\_CH=CH";
break;
case Language.English:
data = $"Item\_Code\_SAP={
sap}&&Plant\_SAP=6000&EN\_CH=EN";
break;
case Language.German:
data = $"Item\_Code\_SAP={
sap}&&Plant\_SAP=6000&EN\_CH=DE";
break;
}
using (var client = new RestClient(url + data))
{
var request = new RestRequest();
request.Method = Method.Get;
request.Timeout = 3000;
var response = client.Execute(request);
var result = response.Content;
Console.WriteLine(result);
Console.ReadLine();
}
}
返回Get The response string for the request,测试OK.
边栏推荐
猜你喜欢

When does MySQL use table locks and when does it use row locks?

Universal js time date format conversion

2020 ACM | MoFlow: An Invertible Flow Model for Generating Molecular Graphs

VR机器人教你如何正确打乒乓球

这个终端连接工具,碾压Xshell

LSF提交作业命令--bsub

selenium模块

DNS domain name resolution services

DP5340国产替代CM5340立体声音频A/D转换器芯片

How does Redis prevent oversold and inventory deduction operations?
随机推荐
Mobile phone side scroll to page to specify location
一段神奇的没有主方法的代码
Calculate the inverse source of the matrix (using the adjoint matrix, a 3x3 matrix)
人工肌肉智能材料新突破
万能js时间日期格式转换
What are the access modifiers, declaration modifiers, and keywords in C#?Literacy articles
Goto statements
Interview with Ant: How do these technology pioneers do the bottom-level development well?| Excellent technical team interview
k8s 部署mysql8(PV和PVC 版本)
Universal js time date format conversion
ArrayList
限塑令下的新材料——聚乳酸(PLA)
interface
this与super
Go uses freecache for caching
go : go-redis set操作
C language custom types, rounding
New material under the plastic restriction order - polylactic acid (PLA)
selenium模块
学生成绩管理系统(C语言)