修改
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Net;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
@@ -97,7 +98,7 @@ public static class HttpHelper
|
||||
string url,
|
||||
HttpMethod method,
|
||||
Dictionary<string, string>? headers = null,
|
||||
Dictionary<string, string>? cookies = null,
|
||||
string? cookies = null,
|
||||
HttpContent? content = null,
|
||||
int timeoutSeconds = 30,
|
||||
string? proxy = null,
|
||||
@@ -148,9 +149,9 @@ public static class HttpHelper
|
||||
}
|
||||
}
|
||||
|
||||
if (cookies is { Count: > 0 })
|
||||
if (string.IsNullOrWhiteSpace(cookies))
|
||||
{
|
||||
request.Headers.TryAddWithoutValidation("Cookie", string.Join("; ", cookies.Select(c => $"{c.Key}={c.Value}")));
|
||||
request.Headers.TryAddWithoutValidation("Cookie", cookies);
|
||||
}
|
||||
|
||||
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
@@ -197,21 +198,45 @@ public static class HttpHelper
|
||||
}
|
||||
}
|
||||
|
||||
// 你的三个快捷方法完全不变
|
||||
|
||||
|
||||
public static Dictionary<string, string> ParseQueryString(string query)
|
||||
{
|
||||
if (string.IsNullOrEmpty(query))
|
||||
return new Dictionary<string, string>();
|
||||
|
||||
return query.Split('&')
|
||||
.Select(x => x.Split('='))
|
||||
.ToDictionary(
|
||||
x => Uri.UnescapeDataString(x[0]),
|
||||
x => Uri.UnescapeDataString(x.Length > 1 ? x[1] : "")
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
// 你的4个快捷方法完全不变
|
||||
public static Task<HttpResult> GetAsync(string url, Dictionary<string, string>? headers = null,
|
||||
Dictionary<string, string>? cookies = null, int timeoutSeconds = 30, string? proxy = null) =>
|
||||
string? cookies = null, int timeoutSeconds = 30, string? proxy = null) =>
|
||||
SendAsync(url, HttpMethod.Get, headers, cookies, null, timeoutSeconds, proxy);
|
||||
|
||||
public static Task<HttpResult> PostFormAsync(string url, Dictionary<string, string> form,
|
||||
Dictionary<string, string>? headers = null, Dictionary<string, string>? cookies = null,
|
||||
Dictionary<string, string>? headers = null, string? cookies = null,
|
||||
int timeoutSeconds = 30, string? proxy = null)
|
||||
{
|
||||
var content = new FormUrlEncodedContent(form);
|
||||
return SendAsync(url, HttpMethod.Post, headers, cookies, content, timeoutSeconds, proxy);
|
||||
}
|
||||
|
||||
public static Task<HttpResult> PostFormAsync(string url, string form,
|
||||
Dictionary<string, string>? headers = null, string? cookies = null,
|
||||
int timeoutSeconds = 30, string? proxy = null)
|
||||
{
|
||||
var content = new FormUrlEncodedContent(ParseQueryString(form));
|
||||
return SendAsync(url, HttpMethod.Post, headers, cookies, content, timeoutSeconds, proxy);
|
||||
}
|
||||
|
||||
public static Task<HttpResult> PostJsonAsync(string url, string json,
|
||||
Dictionary<string, string>? headers = null, Dictionary<string, string>? cookies = null,
|
||||
Dictionary<string, string>? headers = null, string? cookies = null,
|
||||
int timeoutSeconds = 30, string? proxy = null)
|
||||
{
|
||||
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
||||
|
||||
Reference in New Issue
Block a user