当前位置:网站首页>C#通用接口调用
C#通用接口调用
2022-07-03 02:58:00 【Mnybgm】
项目场景:
提示:C#通用接口调用
解决方案:
提示:先获取令牌,然后调用数据接口:
/// <summary>
/// 获取Token值
/// </summary>
/// <returns>token值</returns>
public string InterfaceToken()
{
HttpWebRequest request = HttpWebRequest.CreateHttp("http://主机/token?client_id=xxxxxx";);
request.ContentType = "application/json";//body数据类型
request.Method = "GET";
var response = (HttpWebResponse)request.GetResponse();
var resuleJson = string.Empty;//返回的json数据
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("UTF-8")))
{
resuleJson = reader.ReadToEnd();
}
}
TokenData tokenData = new TokenData();
tokenData = JsonConvert.DeserializeObject<TokenData>(resuleJson);
var aaa= JsonConvert.DeserializeObject(resuleJson);
return tokenData.access_token;
}
public class TokenData
{
public string access_token { get; set; }
}
调用接口:
/// <summary>
/// 接口数据获取
/// </summary>
/// <param name="citgsOperation">物料组</param>
/// <param name="cpclOperation">分类码</param>
/// <param name="mitm">物料代码</param>
public void InterfaceData(string mitm = "")
{
string citgs = "[]";//citgs 物料组 false
int citgsOperation = 0;//citgsOperation物料组过滤操作 true 0
int cpclOperation = 0;//cpclOperation 分类码过滤操作 true 0
string cpcls = "[]";//cpcls分类码 false
string level = "1";//level需要展开的BOM的层数 false
//string mitm = "";//mitm物料代码 true
string queryDate = "";//queryDate查询时间 false
string token = InterfaceToken();
string data = "{\"citgs\": " + citgs + ",\"citgsOperation\":" + citgsOperation + ",\"cpclOperation\": " + cpclOperation + ",\"cpcls\":" + cpcls + ",\"level\":" + level + ",\"mitm\":\" " + mitm + "\",\"queryDate\": \"" + queryDate + "\"}";
HttpWebRequest request = HttpWebRequest.CreateHttp("http://主机";);
request.ContentType = "application/json";//body数据类型
request.Headers.Add("x-authorization", token);
request.Method = "POST";
using (var postStream = new StreamWriter(request.GetRequestStream()))
{
postStream.Write(data);
}
var response = (HttpWebResponse)request.GetResponse();
var resuleJson = string.Empty;//返回的json数据
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("UTF-8")))
{
resuleJson = reader.ReadToEnd();
}
}
}
边栏推荐
- Do you really understand relays?
- HTB-Devel
- The left value and the right finger explain better
- Summary of interview project technology stack
- Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
- Global and Chinese ammonium dimolybdate market in-depth analysis and prospect risk prediction report 2022 Edition
- 销毁Session和清空指定的属性
- Can netstat still play like this?
- [principles of multithreading and high concurrency: 1_cpu multi-level cache model]
- Strategy application of Dameng database
猜你喜欢
TCP handshake three times and wave four times. Why does TCP need handshake three times and wave four times? TCP connection establishes a failure processing mechanism
用docker 连接mysql的过程
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
超好用的日志库 logzero
Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
Kubernetes family container housekeeper pod online Q & A?
Check log4j problems using stain analysis
Installation and use of memory leak tool VLD
Three.js本地环境搭建
随机推荐
模糊查询时报错Parameter index out of range (1 > number of parameters, which is 0)
Cron表达式介绍
后管中编辑与预览获取表单的值写法
A2L file parsing based on CAN bus (2)
Cancer biopsy instruments and kits - market status and future development trends
当lambda没有输入时,是何含义?
How to implement append in tensor
Process the dataset and use labelencoder to convert all IDs to start from 0
JS finds all the parent nodes or child nodes under a node according to the tree structure
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
Do you really understand relays?
The base value is too large (the error is marked as "08") [duplicate] - value too great for base (error token is'08') [duplicate]
TCP handshake three times and wave four times. Why does TCP need handshake three times and wave four times? TCP connection establishes a failure processing mechanism
[shutter] banner carousel component (shutter_wiper plug-in | swiper component)
I2C 子系統(四):I2C debug
[fluent] futurebuilder asynchronous programming (futurebuilder construction method | asyncsnapshot asynchronous calculation)
Distributed transaction
SQL statement
Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)