72 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			72 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.Net;
 | |
| using RestSharp;
 | |
| 
 | |
| namespace BLL
 | |
| {
 | |
|     public static class HttpHelper
 | |
|     {
 | |
|         /// <summary>
 | |
|         /// GET请求
 | |
|         /// </summary>
 | |
|         /// <param name="url"></param>
 | |
|         /// <param name="token"></param>
 | |
|         /// <returns></returns>
 | |
| 
 | |
|         public static string Get(string url, Dictionary<string, string> token)
 | |
|         {
 | |
|             ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
 | |
| 
 | |
|             var client = new RestClient(url);
 | |
|             client.Timeout = -1;
 | |
|             var request = new RestRequest(Method.GET);
 | |
|             if (token != null)
 | |
|             {
 | |
|                 foreach (var item in token)
 | |
|                 {
 | |
|                     request.AddHeader(item.Key, item.Value);
 | |
|                 }
 | |
|             }
 | |
|             request.AddHeader("ClientId", SysConstSetService.ClientId);
 | |
|             request.AddHeader("OperationCode", url.Substring(url.LastIndexOf("/", StringComparison.Ordinal) + 1));
 | |
|             IRestResponse response = client.Execute(request);
 | |
|             return response.Content;
 | |
|         }
 | |
| 
 | |
| 
 | |
|         /// <summary>
 | |
|         /// POST请求
 | |
|         /// </summary>
 | |
|         /// <param name="url"></param>
 | |
|         /// <param name="token"></param>
 | |
|         /// <param name="jsonBody"></param>
 | |
|         /// <returns></returns>
 | |
|         public static string Post(string url, Dictionary<string, string> token, string jsonBody)
 | |
|         {
 | |
|             ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
 | |
| 
 | |
|             var client = new RestClient(url);
 | |
|             client.Timeout = -1;
 | |
|             var request = new RestRequest(Method.POST);
 | |
|             if (token != null)
 | |
|             {
 | |
|                 foreach (var item in token)
 | |
|                 {
 | |
|                     request.AddHeader(item.Key, item.Value);
 | |
|                 }
 | |
|             }
 | |
|             request.AddHeader("ClientId", SysConstSetService.ClientId);
 | |
|             request.AddHeader("OperationCode", url.Substring(url.LastIndexOf("/", StringComparison.Ordinal) + 1));
 | |
|             if (!string.IsNullOrEmpty(jsonBody))
 | |
|             {
 | |
|                 request.AddJsonBody(jsonBody);
 | |
|             }
 | |
| 
 | |
|             IRestResponse response = client.Execute(request);
 | |
|             return response.Content;
 | |
|         }
 | |
| 
 | |
| 
 | |
|     }
 | |
| } |