12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Text;
- using System.Threading.Tasks;
- using XdCxRhDw.Dto;
- namespace XdCxRhDW.Core
- {
- public class HttpHelper
- {
- /// <summary>
- ///
- /// </summary>
- /// <param name="url"></param>
- /// <param name="data"></param>
- /// <returns></returns>
- public static string PostRequest(string url, HttpContent data)
- {
- var handler = new HttpClientHandler() { UseCookies = false };
- HttpClient client = new HttpClient(handler);
- client.Timeout = TimeSpan.FromSeconds(10);
- var message = new HttpRequestMessage(HttpMethod.Post, url);
- message.Content = data;
- var response = client.SendAsync(message).Result;
- response.EnsureSuccessStatusCode();
- var result = response.Content.ReadAsStringAsync().Result;
- return result;
- }
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="url"></param>
- /// <param name="data"></param>
- /// <returns></returns>
- public static T PostRequest<T>(string url, HttpContent data)
- {
- var handler = new HttpClientHandler() { UseCookies = false };
- HttpClient client = new HttpClient(handler);
- client.Timeout = TimeSpan.FromSeconds(20);
- var message = new HttpRequestMessage(HttpMethod.Post, url);
- message.Content = data;
- var response = client.SendAsync(message).Result;
- response.EnsureSuccessStatusCode();
- var result = response.Content.ReadAsStringAsync().Result;
- var AjaxResult = JsonConvert.DeserializeObject<AjaxResult<T>>(result);
- if (AjaxResult.code == 200)
- {
- return AjaxResult.data;
- }
- else
- {
- throw new Exception(AjaxResult.msg);
- }
- }
- }
- }
|