using RestSharp; using System; using System.Collections.Generic; using System.Threading.Tasks; using XXCpzs.Models; namespace XXCpzs.helper { public class HttpHelper { public static async Task RequestPost(int type, Dictionary _body = null) { string _domain = string.Format(@"http://cpzs.xixingwl.cn/cpzs/Api/{0}.shtml", type); Console.WriteLine(">>>>" + _domain); var client = new RestClient { BaseUrl = new Uri(_domain), Timeout = 60000, //Authenticator = new HttpBasicAuthenticator("username", "password") }; var request = new RestRequest { Timeout = 30000, Method = Method.POST }; request.AddHeader("Content-Type", "application/json"); request.AddHeader("Authorization", "Bearer123456"); request.AddJsonBody(_body); var response = await client.ExecuteAsync(request); Console.WriteLine(response.Content); if (response.IsSuccessful) { return response.Data; } return new ResultModel { Code = 500, Msg = "系统错误!" }; ; } /// send the get request based on HttpWebRequest /// /// the url you post /// the parameters you post /// return a response object public static List RefreshData(int type, Method method = Method.GET, Dictionary _body = null) { string _domain = string.Format(@"http://cpzs.xixingwl.cn/cpzs/Api/{0}.shtml", type); Console.WriteLine(">>>>" + _domain); var client = new RestClient { BaseUrl = new Uri(_domain), Timeout = 60000, //Authenticator = new HttpBasicAuthenticator("username", "password") }; var request = new RestRequest { Timeout = 30000, Method = method }; request.AddHeader("Content-Type", "application/json"); request.AddHeader("Authorization", "Bearer123456"); request.AddJsonBody(_body); IRestResponse> response2 = client.Execute>(request); Console.WriteLine(response2.Content); if (response2.Data.Code == 200 && response2.Data.Data != null) { return response2.Data.Data; } return null; } public static BaseModel Refresh(string type, Dictionary _body = null) { string _domain = string.Format(@"http://cpzs.xixingwl.cn/addons/cpzs/Api/{0}.shtml", type); Console.WriteLine(">>>>" + _domain); var client = new RestClient { BaseUrl = new Uri(_domain), Timeout = 60000, //Authenticator = new HttpBasicAuthenticator("username", "password") }; var request = new RestRequest { Timeout = 30000, Method = Method.POST }; request.AddHeader("Content-Type", "application/json"); request.AddHeader("Authorization", "Bearer123456"); request.AddJsonBody(_body); IRestResponse> response2 = client.Execute>(request); if (response2.IsSuccessful) { Console.Write(">>>" + response2.Content); return response2.Data; } return new BaseModel() { Code = 500, Msg = "系统错误!" }; } } public enum RequestType { /// /// 授权验证 /// noun, /// /// 设备软件验证 /// verb, /// /// 注册接口 /// reg ,//= "reg", /// /// 获取部门 /// getdepartment } }