当前位置:网站首页>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.
边栏推荐
- Hex conversion...
- 识别“数据陷阱”,发现数据的可疑之处
- The first artificial intelligence safety competition officially launched
- Architectural Design Guide How to Become an Architect
- redis实现分布式锁的原理
- 雷总个人博客看到
- 限塑令下的新材料——聚乳酸(PLA)
- Headline 2: there are several kinds of common SQL errors in MySQL usage?
- UDP和TCP使用同一个端口,可行吗?
- [GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及
猜你喜欢
The first artificial intelligence safety competition officially launched
和AI一起玩儿剧本杀:居然比我还入戏
深度学习:线性回归模型
【MySQL】MySQL中如何实现分页操作
Get all interface paths and names in the controller
What new materials are used in the large aircraft C919?
selenium module
DP5340 domestic replacement for CM5340 stereo audio A/D converter chip
ETL为什么经常变成ELT甚至LET?
如何实时计算日累计逐单资金流
随机推荐
MySQL basics [naming convention]
The calculation and source code of the straight line intersecting the space plane
Electron中设置菜单(Menu),主进程向渲染进程共享数据
this and super
Develop common tool software
Go 使用 freecache 缓存
【MySQL】MySQL中如何实现分页操作
MySQL master-slave replication configuration construction, one step in place
sql concat()函数
头条二面:MySQL中有几种常见的 SQL 错误用法?
专访蚂蚁:这群技术排头兵,如何做好底层开发这件事?| 卓越技术团队访谈录
interface
使用navicat连接mysql数据库时常报的错误:2003、1698、1251
2020 ACM | MoFlow: An Invertible Flow Model for Generating Molecular Graphs
包含min函数的栈(js)
go : go-redis list operation
redis实现分布式锁的原理
人工肌肉智能材料新突破
Go: go - redis based operation
How does Redis prevent oversold and inventory deduction operations?