若干更新
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
|
||||
namespace Ramitta.lib
|
||||
@@ -14,8 +15,6 @@ namespace Ramitta.lib
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||
request.Method = "GET";
|
||||
request.Timeout = timeout;
|
||||
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36";
|
||||
|
||||
using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
|
||||
using (Stream stream = response.GetResponseStream())
|
||||
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
|
||||
@@ -29,44 +28,56 @@ namespace Ramitta.lib
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<string> SendHttpPostAsync(string url, string jsonData, int timeout = 10000)
|
||||
public static async Task<string> SendHttpPostAsync(string url, string jsonData, int timeout = 10000, bool bypassProxy = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
||||
request.Method = "POST";
|
||||
request.Timeout = timeout;
|
||||
request.ContentType = "application/json";
|
||||
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
|
||||
var handler = new HttpClientHandler();
|
||||
|
||||
byte[] data = Encoding.UTF8.GetBytes(jsonData);
|
||||
request.ContentLength = data.Length;
|
||||
|
||||
using (Stream requestStream = await request.GetRequestStreamAsync())
|
||||
if (bypassProxy)
|
||||
{
|
||||
await requestStream.WriteAsync(data, 0, data.Length);
|
||||
// 方法1:不使用代理
|
||||
handler.UseProxy = false;
|
||||
handler.Proxy = null;
|
||||
|
||||
// 方法2:指定某些地址不代理
|
||||
// handler.Proxy = new WebProxy();
|
||||
// handler.UseProxy = true;
|
||||
// handler.PreAuthenticate = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 使用系统代理
|
||||
handler.UseProxy = true;
|
||||
handler.Proxy = WebRequest.GetSystemWebProxy();
|
||||
}
|
||||
|
||||
using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
|
||||
using (Stream responseStream = response.GetResponseStream())
|
||||
using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
|
||||
// 可选:禁用自动重定向
|
||||
handler.AllowAutoRedirect = false;
|
||||
|
||||
using (var client = new HttpClient(handler))
|
||||
{
|
||||
return await reader.ReadToEndAsync();
|
||||
client.Timeout = TimeSpan.FromMilliseconds(timeout);
|
||||
var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
|
||||
var response = await client.PostAsync(url, content);
|
||||
|
||||
// 确保成功状态
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorContent = await response.Content.ReadAsStringAsync();
|
||||
throw new Exception($"HTTP错误 {(int)response.StatusCode} ({response.StatusCode}): {errorContent}");
|
||||
}
|
||||
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
}
|
||||
catch (WebException ex)
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
WebResponse errorResponse = ex.Response;
|
||||
if (errorResponse is HttpWebResponse httpErrorResponse)
|
||||
{
|
||||
using (Stream errorStream = httpErrorResponse.GetResponseStream())
|
||||
using (StreamReader errorReader = new StreamReader(errorStream, Encoding.UTF8))
|
||||
{
|
||||
string errorContent = await errorReader.ReadToEndAsync();
|
||||
throw new Exception($"HTTP错误 {(int)httpErrorResponse.StatusCode} ({httpErrorResponse.StatusCode}): {errorContent}");
|
||||
throw new Exception("HTTP请求失败: " + ex.Message);
|
||||
}
|
||||
}
|
||||
throw new Exception("网络错误: " + ex.Message);
|
||||
catch (TaskCanceledException ex)
|
||||
{
|
||||
throw new Exception("请求超时: " + ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
|
||||
Reference in New Issue
Block a user