Files
xxcpzs/XXCpzs/Helper/HttpHelper.cs
2020-04-03 11:55:02 +08:00

133 lines
4.3 KiB
C#

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<ResultModel> RequestPost(int type, Dictionary<string, string> _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<ResultModel>(request);
Console.WriteLine(response.Content);
if (response.IsSuccessful)
{
return response.Data;
}
return new ResultModel { Code = 500, Msg = "系统错误!" }; ;
}
/// send the get request based on HttpWebRequest
/// </summary>
/// <param name="requestUrl">the url you post</param>
/// <param name="routeParameters">the parameters you post</param>
/// <returns>return a response object</returns>
public static List<T> RefreshData<T>(int type, Method method = Method.GET, Dictionary<string, string> _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<ListModel<T>> response2 = client.Execute<ListModel<T>>(request);
Console.WriteLine(response2.Content);
if (response2.Data.Code == 200 && response2.Data.Data != null)
{
return response2.Data.Data;
}
return null;
}
public static BaseModel<T> Refresh<T>(string type, Dictionary<string, string> _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<BaseModel<T>> response2 = client.Execute<BaseModel<T>>(request);
if (response2.IsSuccessful)
{
Console.Write(">>>" + response2.Content);
return response2.Data;
}
return new BaseModel<T>() { Code = 500, Msg = "系统错误!" };
}
}
public enum RequestType {
/// <summary>
/// 授权验证
/// </summary>
noun,
/// <summary>
/// 设备软件验证
/// </summary>
verb,
/// <summary>
/// 注册接口
/// </summary>
reg ,//= "reg",
/// <summary>
/// 获取部门
/// </summary>
getdepartment
}
}