using Microsoft.Extensions.Logging;
|
using Newtonsoft.Json;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Net;
|
using System.Net.Http.Headers;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEA_Core.LogHelper;
|
|
namespace WIDESEA_Core.Helper
|
{
|
public class HttpHelper
|
{
|
public static async Task<string> GetAsync(string serviceAddress, Dictionary<string, string>? headers = null)
|
{
|
string result = string.Empty;
|
DateTime beginDate = DateTime.Now;
|
try
|
{
|
using HttpClient httpClient = new HttpClient();
|
httpClient.Timeout = new TimeSpan(0, 0, 60);
|
|
if (headers != null)
|
{
|
foreach (var header in headers)
|
httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
|
}
|
|
result = await httpClient.GetAsync(serviceAddress).Result.Content.ReadAsStringAsync();
|
return result;
|
}
|
catch (Exception e)
|
{
|
throw new Exception(e.Message);
|
}
|
finally
|
{
|
Logger.Add(serviceAddress, "", result, beginDate);
|
}
|
}
|
|
public static async Task<string?> PostAsync(string serviceAddress, string requestJson = "", string contentType = "application/json", Dictionary<string, string>? headers = null)
|
{
|
string result = string.Empty;
|
DateTime beginDate = DateTime.Now;
|
try
|
{
|
using (HttpContent httpContent = new StringContent(requestJson))
|
{
|
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
using HttpClient httpClient = new HttpClient();
|
httpClient.Timeout = new TimeSpan(0, 0, 60);
|
|
if (headers != null)
|
{
|
foreach (var header in headers)
|
httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
|
}
|
|
result = await httpClient.PostAsync(serviceAddress, httpContent).Result.Content.ReadAsStringAsync();
|
}
|
return result;
|
}
|
catch (Exception e)
|
{
|
throw new Exception(e.Message);
|
}
|
finally
|
{
|
Logger.Add(serviceAddress, requestJson, result, beginDate);
|
}
|
}
|
|
public static string Get(string serviceAddress, string contentType = "application/json", Dictionary<string, string>? headers = null)
|
{
|
string result = string.Empty;
|
DateTime beginDate = DateTime.Now;
|
try
|
{
|
using HttpClient httpClient = new HttpClient();
|
httpClient.Timeout = new TimeSpan(0, 0, 60);
|
|
if (headers != null)
|
{
|
foreach (var header in headers)
|
httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
|
}
|
|
result = httpClient.GetStringAsync(serviceAddress).Result;
|
return result;
|
}
|
catch (Exception e)
|
{
|
throw new Exception(e.Message);
|
}
|
finally
|
{
|
Logger.Add(serviceAddress, "", result, beginDate);
|
}
|
}
|
|
public static string Post(string serviceAddress, string requestJson = "", string contentType = "application/json", Dictionary<string, string>? headers = null)
|
{
|
string result = string.Empty;
|
DateTime beginDate = DateTime.Now;
|
try
|
{
|
using (HttpContent httpContent = new StringContent(requestJson))
|
{
|
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
using HttpClient httpClient = new HttpClient();
|
httpClient.Timeout = new TimeSpan(0, 0, 60);
|
|
if (headers != null)
|
{
|
foreach (var header in headers)
|
httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
|
}
|
|
result = httpClient.PostAsync(serviceAddress, httpContent).Result.Content.ReadAsStringAsync().Result;
|
}
|
return result;
|
}
|
catch (Exception e)
|
{
|
throw new Exception(e.Message);
|
}
|
finally
|
{
|
Logger.Add(serviceAddress, requestJson, result, beginDate);
|
}
|
}
|
|
/// <summary>
|
/// POST请求(异步版本)- 解决405问题
|
/// </summary>
|
/// <param name="serviceAddress">服务地址</param>
|
/// <param name="requestJson">请求JSON</param>
|
/// <param name="contentType">内容类型</param>
|
/// <param name="headers">请求头</param>
|
/// <returns>响应结果</returns>
|
/// <summary>
|
/// POST请求(同步版本,兼容原有代码)
|
/// </summary>
|
// 如果上面的方法还有问题,使用这个简化版本
|
public static string PostSync(string serviceAddress, string requestJson = "", string contentType = "application/json", Dictionary<string, string>? headers = null)
|
{
|
string result = string.Empty;
|
DateTime beginDate = DateTime.Now;
|
try
|
{
|
using (var httpContent = new StringContent(requestJson, Encoding.UTF8, contentType))
|
using (var httpClient = new HttpClient())
|
{
|
httpClient.Timeout = TimeSpan.FromSeconds(600);
|
|
// 设置默认请求头 - 使用简单的 User-Agent
|
httpClient.DefaultRequestHeaders.Add("Accept", "*/*");
|
httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");
|
httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
|
httpClient.DefaultRequestHeaders.Add("User-Agent", "WMS-Client/1.0"); // 简单的 User-Agent
|
|
if (headers != null)
|
{
|
foreach (var header in headers)
|
httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
|
}
|
|
var response = httpClient.PostAsync(serviceAddress, httpContent).Result;
|
|
Console.WriteLine($"HTTP状态码: {(int)response.StatusCode} - {response.StatusCode}");
|
|
if (!response.IsSuccessStatusCode)
|
{
|
throw new HttpRequestException($"HTTP请求失败: {(int)response.StatusCode} - {response.StatusCode}");
|
}
|
|
result = response.Content.ReadAsStringAsync().Result;
|
return result;
|
}
|
}
|
catch (Exception e)
|
{
|
throw new Exception($"POST请求失败: {e.Message}");
|
}
|
finally
|
{
|
Logger.Add(serviceAddress, requestJson, result, beginDate);
|
}
|
}
|
|
|
/// <summary>
|
/// GET请求
|
/// </summary>
|
/// <param name="serviceAddress">服务地址</param>
|
/// <param name="parameters">请求参数</param>
|
/// <param name="headers">请求头</param>
|
/// <returns>响应结果</returns>
|
public static string GetRequest(string serviceAddress, Dictionary<string, string>? parameters = null, Dictionary<string, string>? headers = null)
|
{
|
string result = string.Empty;
|
DateTime beginDate = DateTime.Now;
|
try
|
{
|
using (var httpClient = new HttpClient())
|
{
|
httpClient.Timeout = TimeSpan.FromSeconds(60);
|
|
// 设置请求头
|
httpClient.DefaultRequestHeaders.Add("Accept", "*/*");
|
httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");
|
httpClient.DefaultRequestHeaders.Add("Connection", "keep-alive");
|
httpClient.DefaultRequestHeaders.Add("User-Agent", "WMS-Client/1.0");
|
|
if (headers != null)
|
{
|
foreach (var header in headers)
|
httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
|
}
|
|
// 构建带参数的 URL
|
string url = serviceAddress;
|
if (parameters != null && parameters.Count > 0)
|
{
|
var queryParams = string.Join("&", parameters.Select(p => $"{Uri.EscapeDataString(p.Key)}={Uri.EscapeDataString(p.Value)}"));
|
url += (url.Contains("?") ? "&" : "?") + queryParams;
|
}
|
|
var response = httpClient.GetAsync(url).Result;
|
|
Console.WriteLine($"GET 状态码: {(int)response.StatusCode} - {response.StatusCode}");
|
|
if (!response.IsSuccessStatusCode)
|
{
|
throw new HttpRequestException($"GET 请求失败: {(int)response.StatusCode} - {response.StatusCode}");
|
}
|
|
result = response.Content.ReadAsStringAsync().Result;
|
return result;
|
}
|
}
|
catch (Exception e)
|
{
|
throw new Exception($"GET 请求失败: {e.Message}");
|
}
|
finally
|
{
|
Logger.Add(serviceAddress, parameters != null ? JsonConvert.SerializeObject(parameters) : "No Parameters", result, beginDate);
|
}
|
}
|
}
|
}
|