123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using System;
- using System.Collections.Generic;
- using System.Net.Http;
- using System.Text;
- using System.Threading.Tasks;
- namespace TEAMModelOS.SDK.Helper.Network.HttpHelper
- {
- public class HttpHelper
- {
- /// <summary>
- /// 同步GET请求
- /// </summary>
- /// <param name="url"></param>
- /// <param name="headers"></param>
- /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
- /// <returns></returns>
- public static string HttpGet(string url, Dictionary<string, string> headers = null, int timeout = 0)
- {
- HttpClient client = new HttpClient();
- if (headers != null)
- {
- foreach (KeyValuePair<string, string> header in headers)
- {
- client.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
- }
- if (timeout > 0)
- {
- client.Timeout = new TimeSpan(0, 0, timeout);
- }
- Byte[] resultBytes = client.GetByteArrayAsync(url).Result;
- return Encoding.UTF8.GetString(resultBytes);
- }
- /// <summary>
- /// 异步GET请求
- /// </summary>
- /// <param name="url"></param>
- /// <param name="headers"></param>
- /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
- /// <returns></returns>
- public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null, int timeout = 0)
- {
- HttpClient client = new HttpClient();
- if (headers != null)
- {
- foreach (KeyValuePair<string, string> header in headers)
- {
- client.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
- }
- if (timeout > 0)
- {
- client.Timeout = new TimeSpan(0, 0, timeout);
- }
- Byte[] resultBytes = await client.GetByteArrayAsync(url);
- return Encoding.Default.GetString(resultBytes);
- }
- /// <summary>
- /// 同步POST请求
- /// </summary>
- /// <param name="url"></param>
- /// <param name="postData"></param>
- /// <param name="headers"></param>
- /// <param name="contentType"></param>
- /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
- /// <param name="encoding">默认UTF8</param>
- /// <returns></returns>
- public static string HttpPost(string url, string postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
- {
- HttpClient client = new HttpClient();
- if (headers != null)
- {
- foreach (KeyValuePair<string, string> header in headers)
- {
- client.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
- }
- if (timeout > 0)
- {
- client.Timeout = new TimeSpan(0, 0, timeout);
- }
- HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8);
- if (contentType != null)
- {
- content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
- }
- HttpResponseMessage responseMessage = client.PostAsync(url, content).Result;
- Byte[] resultBytes = responseMessage.Content.ReadAsByteArrayAsync().Result;
- return Encoding.UTF8.GetString(resultBytes);
- }
- /// <summary>
- /// 异步POST请求
- /// </summary>
- /// <param name="url"></param>
- /// <param name="postData"></param>
- /// <param name="headers"></param>
- /// <param name="contentType"></param>
- /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
- /// <param name="encoding">默认UTF8</param>
- /// <returns></returns>
- public static async Task<string> HttpPostAsync(string url, string postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
- {
- HttpClient client = new HttpClient();
- if (headers != null)
- {
- foreach (KeyValuePair<string, string> header in headers)
- {
- client.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
- }
- if (timeout > 0)
- {
- client.Timeout = new TimeSpan(0, 0, timeout);
- }
- HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8);
- if (contentType != null)
- {
- content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
- }
- HttpResponseMessage responseMessage = await client.PostAsync(url, content);
- Byte[] resultBytes = await responseMessage.Content.ReadAsByteArrayAsync();
- return Encoding.UTF8.GetString(resultBytes);
- }
- /// <summary>
- /// 异步POST请求
- /// </summary>
- /// <param name="url"></param>
- /// <param name="postData"></param>
- /// <param name="headers"></param>
- /// <param name="contentType"></param>
- /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
- /// <param name="encoding">默认UTF8</param>
- /// <returns></returns>
- public static async Task<string> HttpPostAsync(string url, List<KeyValuePair<string, string>> postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
- {
- HttpClient client = new HttpClient();
- if (headers != null)
- {
- foreach (KeyValuePair<string, string> header in headers)
- {
- client.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
- }
- if (timeout > 0)
- {
- client.Timeout = new TimeSpan(0, 0, timeout);
- }
- HttpContent content = new FormUrlEncodedContent(postData);
- if (contentType != null)
- {
- content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
- }
- HttpResponseMessage responseMessage = await client.PostAsync(url, content);
- Byte[] resultBytes = await responseMessage.Content.ReadAsByteArrayAsync();
- return Encoding.UTF8.GetString(resultBytes);
- }
- }
- }
|