当前位置:网站首页>C# 使用RestSharp 实现Get,Post 请求(2)
C# 使用RestSharp 实现Get,Post 请求(2)
2022-07-30 05:51:00 【flysh05】
今天又重新测试了一下该库,的确很好用,当初为了快捷使用了Python应用,绕了一些。
查看网络实际使用的请求方式,如下图:

封装一个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;
}
}
传入一个变量数据,返回响应的内容,测试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 请求的响应字符串,测试OK。
边栏推荐
- Electron中设置菜单(Menu),主进程向渲染进程共享数据
- RAID磁盘阵列
- 上传文件--文件类型大全,图片类型,文档类型,视频类型,压缩包类型
- 手机端滚动至页面指定位置
- 使用navicat连接mysql数据库时常报的错误:2003、1698、1251
- From catching up to surpassing, domestic software shows its talents
- Vue项目通过node连接MySQL数据库并实现增删改查操作
- AI可通过X光片识别种族,但没人知道为什么
- Equation Derivation Proof of Vector Triple Product
- Linx常见目录&文件管理命令&VI编辑器使用 介绍
猜你喜欢

STL source code analysis: conceptual understanding of iterators, and code testing.

The calculation proof of the intersection of the space line and the plane and its source code

Is it possible to use the same port for UDP and TCP?

阿里二面:Redis有几种集群方案?我答了4种

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

export , export default, import complete usage

LVM和磁盘配额

你被MySQL 中的反斜杠 \\坑过吗?

深度学习:线性回归模型

Go语学习笔记 - gorm使用 - 数据库配置、表新增 Web框架Gin(七)
随机推荐
从 Google 离职,前Go 语言负责人跳槽小公司
首届人工智能安全大赛正式启动
Electron日常学习笔记
分布式系统中的开创者—莱斯利·兰伯特
Boot process and service control
Equation Derivation Proof of Vector Triple Product
go : go gin返回JSON数据
Calculate the inverse source of the matrix (using the adjoint matrix, a 3x3 matrix)
进程和计划任务管理
Distance calculation from space vertex to straight line and its source code
Test development engineer diary 002 - starting from 0 interface automation
Pioneer in Distributed Systems - Leslie Lambert
Graphical relational database design ideas, this is too vivid
STL源码剖析:迭代器的概念理解,以及代码测试。
Ali two sides: List several tips for Api interface optimization
Swagger使用方式,告别postman
Ali Ermian: How many cluster solutions does Redis have?I answered 4
bin文件夹下的roslyn文件夹
How to use Swagger, say goodbye to postman
roslyn folder under bin folder