| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web;
- namespace ReleaseHelper.Utils
- {
- public static class Http
- {
- public struct Response
- {
- public HttpStatusCode code;
- public HttpContent message;
- public string url;
- }
- public struct Request
- {
- /// <summary>
- /// 请求的网址
- /// </summary>
- public string uri;
- /// <summary>
- /// 头信息
- /// </summary>
- public Dictionary<string, object> headers;
- /// <summary>
- /// 请求的数据
- /// </summary>
- public Dictionary<string, object> data;
- /// <summary>
- /// 上传的文件
- /// </summary>
- public Dictionary<string, string> files;
- /// <summary>
- /// 上传的文件二进制流
- /// </summary>
- public Dictionary<string, byte[]> fileStream;
- /// <summary>
- /// 请求类型
- /// </summary>
- public RequestContentType contentType;
- /// <summary>
- /// 请求方式
- /// </summary>
- public Method method;
- public void Init()
- {
- uri = "";
- headers = new Dictionary<string, object>();
- data = new Dictionary<string, object>();
- files = new Dictionary<string, string>();
- fileStream = new Dictionary<string, byte[]>();
- contentType = RequestContentType.Default;
- method = Method.Get;
- }
- public Request(string uri, Method method)
- {
- this.uri = uri;
- this.method = method;
- this.headers = new Dictionary<string, object>();
- this.data = null;
- files = null;
- fileStream = null;
- contentType = RequestContentType.Default;
- }
- public Request(string uri, Method method, Dictionary<string, object> data)
- {
- this.uri = uri;
- this.method = method;
- this.headers = new Dictionary<string, object>();
- this.data = data;
- files = null;
- fileStream = null;
- contentType = RequestContentType.Default;
- }
- public Request(string uri, Method method, Dictionary<string, object> headers, Dictionary<string, object> data)
- {
- this.uri = uri;
- this.method = method;
- this.headers = headers;
- this.data = data;
- files = null;
- fileStream = null;
- contentType = RequestContentType.Default;
- }
- public Request(string uri, Method method, RequestContentType contentType, Dictionary<string, object> headers, Dictionary<string, object> data)
- {
- this.uri = uri;
- this.method = method;
- this.headers = headers;
- this.data = data;
- files = null;
- fileStream = null;
- this.contentType = contentType;
- }
- public Request(string uri, Method method, Dictionary<string, object> headers, Dictionary<string, object> data, Dictionary<string, string> files)
- {
- this.uri = uri;
- this.method = method;
- this.headers = headers;
- this.data = data;
- this.files = files;
- fileStream = null;
- contentType = RequestContentType.Default;
- }
- public Request(string uri, Method method, Dictionary<string, object> headers, Dictionary<string, object> data, Dictionary<string, string> files, Dictionary<string, byte[]> fileStream)
- {
- this.uri = uri;
- this.method = method;
- this.headers = headers;
- this.data = data;
- this.files = files;
- this.fileStream = fileStream;
- contentType = RequestContentType.Default;
- }
- }
- /// <summary>
- /// 请求内容类型
- /// </summary>
- public enum RequestContentType
- {
- Default, Json
- }
- /// <summary>
- /// 请求方法
- /// </summary>
- public enum Method
- {
- Get, Post, Put, Delete, Patch
- }
- /// <summary>
- /// 初始化请求头
- /// </summary>
- /// <param name="httpClient"></param>
- /// <param name="headers"></param>
- private static void InitClientHeaders(ref HttpClient httpClient, Dictionary<string, object> headers)
- {
- if (headers != null)
- {
- foreach (var header in headers)
- {
- if (!string.IsNullOrEmpty(header.Key) && !string.IsNullOrEmpty(header.Value.ToString()))
- {
- httpClient.DefaultRequestHeaders.Add(header.Key, header.Value.ToString());
- }
- }
- }
- }
- /// <summary>
- /// 获取
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public static async Task<HttpResponseMessage> Fetch(Request request)
- {
- HttpResponseMessage response;
- switch (request.method)
- {
- case Method.Post:
- response = await Post(request);
- break;
- case Method.Put:
- response = await Put(request);
- break;
- case Method.Patch:
- response = await Patch(request);
- break;
- case Method.Delete:
- response = await Delete(request);
- break;
- default:
- response = await Get(request);
- break;
- }
- return response;
- }
- public static async Task<HttpResponseMessage> Get(Request request)
- {
- HttpResponseMessage response = null;
- if (!string.IsNullOrEmpty(request.uri))
- {
- HttpClient client = new HttpClient();
- string url = request.uri;
- InitClientHeaders(ref client, request.headers);
- if (request.data != null)
- {
- //client.DefaultRequestHeaders.Add()
- url = string.Concat(url, "?");
- foreach (var item in request.data)
- {
- url = string.Concat(url, $"{item.Key}={HttpUtility.UrlEncode(item.Value.ToString())}&");
- }
- url = url.Substring(0, url.Length - 1);
- }
- try
- {
- response = await client.GetAsync(url);
- }
- catch (Exception ex)
- {
- Log.Error(ex);
- }
- finally
- {
- client?.Dispose();
- }
- }
- return response;
- }
- private static HttpContent InitNonGetContent(Request request)
- {
- HttpContent content = null;
- if (request.contentType == RequestContentType.Json)
- {
- // 发送json 数据
- HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(request.data), Encoding.UTF8);
- httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
- content = httpContent;
- }
- else
- //if (request.files != null || request.fileStream != null)
- {
- // 普通的键值对提交
- MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent();
- //FormUrlEncodedContent formUrlEncodedContent = new FormUrlEncodedContent();
- if (request.files != null)
- {
- foreach (var file in request.files)
- {
- multipartFormDataContent.Add(new ByteArrayContent(File.ReadAllBytes(file.Value)), file.Key, Path.GetFileName(file.Value));
- }
- }
- if (request.fileStream != null)
- {
- foreach (var file in request.fileStream)
- {
- multipartFormDataContent.Add(new ByteArrayContent(file.Value), file.Key, file.Key);
- }
- }
- if (request.data != null)
- {
- foreach (var d in request.data)
- {
- multipartFormDataContent.Add(new StringContent(d.Value.ToString(), Encoding.UTF8), d.Key);
- }
- }
- content = multipartFormDataContent;
- }
- return content;
- }
- public static async Task<HttpResponseMessage> Post(Request request)
- {
- HttpResponseMessage response = null;
- if (!string.IsNullOrEmpty(request.uri))
- {
- HttpClient client = new HttpClient();
- HttpContent content = InitNonGetContent(request);
- try
- {
- InitClientHeaders(ref client, request.headers);
- response = await client.PostAsync(request.uri, content);
- //client.Dispose();
- }
- catch (Exception ex)
- {
- Log.Error(ex);
- }
- finally
- {
- client?.Dispose();
- }
- }
- return response;
- }
- public static async Task<HttpResponseMessage> Patch(Request request)
- {
- HttpResponseMessage response = null;
- if (!string.IsNullOrEmpty(request.uri))
- {
- HttpClient client = new HttpClient();
- HttpContent content = InitNonGetContent(request);
- HttpRequestMessage httpRequestMessage = new HttpRequestMessage(new HttpMethod("PATCH"), request.uri);
- try
- {
- InitClientHeaders(ref client, request.headers);
- httpRequestMessage.Content = content;
- response = await client.SendAsync(httpRequestMessage);
- //client.Dispose();
- }
- catch (Exception ex)
- {
- Log.Error(ex);
- }
- finally
- {
- client?.Dispose();
- }
- httpRequestMessage.Dispose();
- }
- return response;
- }
- public static async Task<HttpResponseMessage> Put(Request request)
- {
- HttpResponseMessage response = null;
- if (!string.IsNullOrEmpty(request.uri))
- {
- HttpClient client = new HttpClient();
- HttpContent content = InitNonGetContent(request);
- try
- {
- InitClientHeaders(ref client, request.headers);
- response = await client.PutAsync(request.uri, content);
- //client.Dispose();
- }
- catch (Exception ex)
- {
- Log.Error(ex);
- }
- finally
- {
- client?.Dispose();
- }
- }
- return response;
- }
- public static async Task<HttpResponseMessage> Delete(Request request)
- {
- HttpResponseMessage response = null;
- if (!string.IsNullOrEmpty(request.uri))
- {
- HttpClient client = new HttpClient();
- string url = request.uri;
- try
- {
- InitClientHeaders(ref client, request.headers);
- if (request.data != null)
- {
- url = string.Concat(url, "?");
- foreach (var item in request.data)
- {
- url = string.Concat(url, $"{item.Key}={HttpUtility.UrlEncode(item.Value.ToString())}&");
- }
- }
- response = await client.DeleteAsync(request.uri);
- //client.Dispose();
- }
- catch (Exception ex)
- {
- Log.Error(ex);
- }
- finally
- {
- client?.Dispose();
- }
- }
- return response;
- }
- }
- }
|