123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web;
- namespace Common
- {
- public class HttpHelper
- {/// <summary>
- /// 发起POST同步请求
- ///
- /// </summary>
- /// <param name="url"></param>
- /// <param name="postData"></param>
- /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
- /// <param name="headers">填充消息头</param>
- /// <returns></returns>
- public static async Task<string> HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
- {
- postData = postData ?? "";
- using (HttpClient client = new HttpClient())
- {
- if (headers != null)
- {
- foreach (var header in headers)
- client.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
- using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
- {
- if (contentType != null)
- httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
- var responseWait = client.PostAsync(url, httpContent);
- HttpResponseMessage response = await responseWait;
- return response.Content.ReadAsStringAsync().Result;
- }
- }
- }
- /// <summary>
- /// 发起POST异步请求
- /// </summary>
- /// <param name="url"></param>
- /// <param name="postData"></param>
- /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
- /// <param name="headers">填充消息头</param>
- /// <returns></returns>
- public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
- {
- postData = postData ?? "";
- using (HttpClient client = new HttpClient())
- {
- client.Timeout = new TimeSpan(0, 0, timeOut);
- if (headers != null)
- {
- foreach (var header in headers)
- client.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
- using (HttpContent httpContent = new StringContent(postData, Encoding.UTF8))
- {
- if (contentType != null)
- httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
- HttpResponseMessage response = await client.PostAsync(url, httpContent);
- return await response.Content.ReadAsStringAsync();
- }
- }
- }
- /// <summary>
- /// 发起GET同步请求
- /// </summary>
- /// <param name="url"></param>
- /// <param name="headers"></param>
- /// <param name="contentType"></param>
- /// <returns></returns>
- public static string HttpGet(string url, string contentType = null, Dictionary<string, string> headers = null)
- {
- using (HttpClient client = new HttpClient())
- {
- if (contentType != null)
- client.DefaultRequestHeaders.Add("ContentType", contentType);
- if (headers != null)
- {
- foreach (var header in headers)
- client.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
- HttpResponseMessage response = client.GetAsync(url).Result;
- return response.Content.ReadAsStringAsync().Result;
- }
- }
- /// <summary>
- /// 发起GET异步请求
- /// </summary>
- /// <param name="url"></param>
- /// <param name="headers"></param>
- /// <param name="contentType"></param>
- /// <returns></returns>
- public static async Task<string> HttpGetAsync(string url, string contentType = null, Dictionary<string, string> headers = null)
- {
- using (HttpClient client = new HttpClient())
- {
- if (contentType != null)
- client.DefaultRequestHeaders.Add("ContentType", contentType);
- if (headers != null)
- {
- foreach (var header in headers)
- client.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
- HttpResponseMessage response = await client.GetAsync(url);
- return await response.Content.ReadAsStringAsync();
- }
- }
- }
- }
|