本文主要是介绍C# 使用RestSharp 开发WebApi client端,实现MES通信,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1, 使用 nuget 添加第三方库
2,准备好服务器的 url ,用户名、密码等信息,上代码:
var options = new RestClientOptions("http://www.ebmes.net:10800/oauth/token");var client = new RestClient(options);var request = new RestRequest();request.Method = Method.Post;request.AddParameter("Content-Type", "application/json");request.AddParameter("username", "aoi_client");request.AddParameter("password", "Aoi_Client_202207");request.AddParameter("grant_type", "password");request.AddParameter("client_id", "aoi_client");request.AddParameter("client_secret", "Aoi202207");var response = await client.PostAsync(request);Console.WriteLine(response.Content);var token = JsonConverer.DeserializeJsonString<Token>(response.Content);_token = token.access_token;Console.WriteLine(_token);
3,通过登录获取到的 token ,用 Post 推送信息:
var options = new RestClientOptions("http://www.ebmes.net:10800/gateway/openapi/dc/eb-cc-loading/save-pressure");var client = new RestClient(options);var request = new RestRequest();request.AddParameter("Content-Type", "application/json");request.AddHeader("Authorization", $"Bearer {_token}");request.AddHeader("ACTOR_USERNAME","aoi_client");request.Method = Method.Post;string body = GetPressuerInfo();request.AddJsonBody(body);var response = await client.PostAsync(request);Console.WriteLine(response);
这篇关于C# 使用RestSharp 开发WebApi client端,实现MES通信的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!