| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Business.Core.Utilities
- {
- public class HttpHelper
- {
- public static string HttpGet(string url, Dictionary<string, string>? headers = null)
- {
- HttpClient client = new HttpClient();
- if (headers != null)
- {
- foreach (var header in headers)
- {
- client.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
- }
- try
- {
- byte[] resultBytes = client.GetByteArrayAsync(url).Result;
- return Encoding.UTF8.GetString(resultBytes);
- }
- catch
- {
- return string.Empty;
- }
- }
- public static string HttpPost(string url, string postData, Dictionary<string, string>? headers = null, string contentType = "", int timeout = 0, Encoding? encoding = null)
- {
- HttpClient client = new HttpClient();
- if (headers != null)
- {
- foreach (var header in headers)
- {
- client.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
- }
- HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8);
- if (!string.IsNullOrWhiteSpace(contentType))
- {
- content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
- }
- try
- {
- using HttpResponseMessage responseMessage = client.PostAsync(url, content).Result;
- byte[] resultBytes = responseMessage.Content.ReadAsByteArrayAsync().Result;
- return Encoding.UTF8.GetString(resultBytes);
- }
- catch
- {
- return string.Empty;
- }
- }
- }
- }
|