mirror of
				https://gitee.com/dotnetchina/OpenAuth.Net.git
				synced 2025-10-25 18:29:01 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			71 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			71 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.IO;
 | |
| using System.Net;
 | |
| using System.Text;
 | |
| using System.Threading.Tasks;
 | |
| 
 | |
| namespace Infrastructure.Utilities
 | |
| {
 | |
|     public class HttpManager
 | |
|     {
 | |
|         public static Task<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
 | |
|         {
 | |
|             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 | |
|             request.Method = "POST";
 | |
|             if (!string.IsNullOrEmpty(contentType))
 | |
|             {
 | |
|                 request.ContentType = contentType;
 | |
|             }
 | |
|             if (headers != null)
 | |
|             {
 | |
|                 foreach (var header in headers)
 | |
|                     request.Headers[header.Key] = header.Value;
 | |
|             }
 | |
| 
 | |
|             try
 | |
|             {
 | |
|                 byte[] bytes = Encoding.UTF8.GetBytes(postData ?? "");
 | |
|                 using (Stream sendStream = request.GetRequestStream())
 | |
|                 {
 | |
|                     sendStream.Write(bytes, 0, bytes.Length);
 | |
|                 }
 | |
| 
 | |
|                 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
 | |
|                 {
 | |
|                     Stream responseStream = response.GetResponseStream();
 | |
|                     StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
 | |
|                     return streamReader.ReadToEndAsync();
 | |
|                 }
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 return Task.FromResult(ex.Message);
 | |
|             }
 | |
| 
 | |
|         }
 | |
|         public static  Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null)
 | |
|         {
 | |
|             try
 | |
|             {
 | |
|                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 | |
|                 if (headers != null)
 | |
|                 {
 | |
|                     foreach (var header in headers)
 | |
|                         request.Headers[header.Key] = header.Value;
 | |
|                 }
 | |
|                 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
 | |
|                 {
 | |
|                     Stream responseStream = response.GetResponseStream();
 | |
|                     StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
 | |
|                     return streamReader.ReadToEndAsync();
 | |
|                 }
 | |
|             }
 | |
|             catch (Exception ex)
 | |
|             {
 | |
|                 return Task.FromResult(ex.Message);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| }
 | 
