[reply]
class HttpWebHelper
{
public static string HttpWebGet(string url, Action<HttpClientHandler, HttpClient, HttpRequestMessage>? reqAction = null, Action<HttpClientHandler, HttpClient, HttpRequestMessage, HttpResponseMessage>? respAction = null)
{
var resp = string.Empty;
HttpWebReqResp(new Uri(url), HttpMethod.Get,
(reqAction != null) ? reqAction : (clientHandler, client, requestMessage) =>
{
requestMessage.Content = new StringContent(string.Empty, System.Text.Encoding.UTF8, System.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/html;charset=UTF-8")) { };
},
(respAction != null) ? respAction : (clientHandler, client, requestMessage, responseMessage) =>
{
resp = responseMessage.Content.ReadAsStringAsync().Result;
}
);
return resp;
}
public static async Task<string> HttpWebGetAsync(string url, Action<HttpClientHandler, HttpClient, HttpRequestMessage>? reqAction = null, Action<HttpClientHandler, HttpClient, HttpRequestMessage, HttpResponseMessage>? respAction = null)
{
var resp = string.Empty;
await HttpWebReqRespAsync(new Uri(url), HttpMethod.Get,
(reqAction != null) ? reqAction : (clientHandler, client, requestMessage) =>
{
requestMessage.Content = new StringContent(string.Empty, System.Text.Encoding.UTF8, System.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/html;charset=UTF-8")) { };
},
(respAction != null) ? respAction : async (clientHandler, client, requestMessage, responseMessage) =>
{
resp = await responseMessage.Content.ReadAsStringAsync();
}
);
return resp;
}
public static string HttpWebPost(string url, string postFieldData, Action<HttpClientHandler, HttpClient, HttpRequestMessage>? reqAction = null, Action<HttpClientHandler, HttpClient, HttpRequestMessage, HttpResponseMessage>? respAction = null)
{
var resp = string.Empty;
HttpWebReqResp(new Uri(url), HttpMethod.Post,
(reqAction != null) ? reqAction : (clientHandler, client, requestMessage) =>
{
requestMessage.Content = new StringContent(postFieldData, System.Text.Encoding.UTF8, System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded;charset=UTF-8")) { };
},
(respAction != null) ? respAction : (clientHandler, client, requestMessage, responseMessage) =>
{
resp = responseMessage.Content.ReadAsStringAsync().Result;
}
);
return resp;
}
public static async Task<string> HttpWebPostAsync(string url, string postFieldData, Action<HttpClientHandler, HttpClient, HttpRequestMessage>? reqAction = null, Action<HttpClientHandler, HttpClient, HttpRequestMessage, HttpResponseMessage>? respAction = null)
{
var resp = string.Empty;
await HttpWebReqRespAsync(new Uri(url), HttpMethod.Post,
(reqAction != null) ? reqAction : (clientHandler, client, requestMessage) =>
{
requestMessage.Content = new StringContent(postFieldData, System.Text.Encoding.UTF8, System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded;charset=UTF-8")) { };
},
(respAction != null) ? respAction : async (clientHandler, client, requestMessage, responseMessage) =>
{
resp = await responseMessage.Content.ReadAsStringAsync();
}
);
return resp;
}
private static void HttpWebReqResp(Uri uri, HttpMethod httpMethod, Action<HttpClientHandler, HttpClient, HttpRequestMessage>? reqAction = null, Action<HttpClientHandler, HttpClient, HttpRequestMessage, HttpResponseMessage>? respAction = null)
{
var httpClientHandler = new HttpClientHandler()
{
AutomaticDecompression = System.Net.DecompressionMethods.All,
SslProtocols = System.Security.Authentication.SslProtocols.None,
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; },
};
var httpClient = new HttpClient(httpClientHandler)
{
Timeout = TimeSpan.FromMilliseconds(60000.0),
};
var httpRequestMessage = new HttpRequestMessage()
{
RequestUri = uri,
Method = httpMethod,
};
if (httpClient.DefaultRequestHeaders.UserAgent.Count <= 0)
{
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36");
}
if (reqAction != null)
{
reqAction(httpClientHandler, httpClient, httpRequestMessage);
}
var httpResponseMessage = httpClient.Send(httpRequestMessage);
if (respAction != null)
{
respAction(httpClientHandler, httpClient, httpRequestMessage, httpResponseMessage);
}
}
private static async Task HttpWebReqRespAsync(Uri uri, HttpMethod httpMethod, Action<HttpClientHandler, HttpClient, HttpRequestMessage>? reqAction = null, Action<HttpClientHandler, HttpClient, HttpRequestMessage, HttpResponseMessage>? respAction = null)
{
var httpClientHandler = new HttpClientHandler()
{
AutomaticDecompression = System.Net.DecompressionMethods.All,
SslProtocols = System.Security.Authentication.SslProtocols.None,
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; },
};
var httpClient = new HttpClient(httpClientHandler)
{
Timeout = TimeSpan.FromMilliseconds(60000.0),
};
var httpRequestMessage = new HttpRequestMessage()
{
RequestUri = uri,
Method = httpMethod,
};
if (httpClient.DefaultRequestHeaders.UserAgent.Count <= 0)
{
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36");
}
if (reqAction != null)
{
reqAction(httpClientHandler, httpClient, httpRequestMessage);
}
var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage);
if (respAction != null)
{
respAction(httpClientHandler, httpClient, httpRequestMessage, httpResponseMessage);
}
}
}
[/reply]
class HttpWebHelper
{
public static string HttpWebGet(string url, Action<HttpClientHandler, HttpClient, HttpRequestMessage>? reqAction = null, Action<HttpClientHandler, HttpClient, HttpRequestMessage, HttpResponseMessage>? respAction = null)
{
var resp = string.Empty;
HttpWebReqResp(new Uri(url), HttpMethod.Get,
(reqAction != null) ? reqAction : (clientHandler, client, requestMessage) =>
{
requestMessage.Content = new StringContent(string.Empty, System.Text.Encoding.UTF8, System.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/html;charset=UTF-8")) { };
},
(respAction != null) ? respAction : (clientHandler, client, requestMessage, responseMessage) =>
{
resp = responseMessage.Content.ReadAsStringAsync().Result;
}
);
return resp;
}
public static async Task<string> HttpWebGetAsync(string url, Action<HttpClientHandler, HttpClient, HttpRequestMessage>? reqAction = null, Action<HttpClientHandler, HttpClient, HttpRequestMessage, HttpResponseMessage>? respAction = null)
{
var resp = string.Empty;
await HttpWebReqRespAsync(new Uri(url), HttpMethod.Get,
(reqAction != null) ? reqAction : (clientHandler, client, requestMessage) =>
{
requestMessage.Content = new StringContent(string.Empty, System.Text.Encoding.UTF8, System.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/html;charset=UTF-8")) { };
},
(respAction != null) ? respAction : async (clientHandler, client, requestMessage, responseMessage) =>
{
resp = await responseMessage.Content.ReadAsStringAsync();
}
);
return resp;
}
public static string HttpWebPost(string url, string postFieldData, Action<HttpClientHandler, HttpClient, HttpRequestMessage>? reqAction = null, Action<HttpClientHandler, HttpClient, HttpRequestMessage, HttpResponseMessage>? respAction = null)
{
var resp = string.Empty;
HttpWebReqResp(new Uri(url), HttpMethod.Post,
(reqAction != null) ? reqAction : (clientHandler, client, requestMessage) =>
{
requestMessage.Content = new StringContent(postFieldData, System.Text.Encoding.UTF8, System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded;charset=UTF-8")) { };
},
(respAction != null) ? respAction : (clientHandler, client, requestMessage, responseMessage) =>
{
resp = responseMessage.Content.ReadAsStringAsync().Result;
}
);
return resp;
}
public static async Task<string> HttpWebPostAsync(string url, string postFieldData, Action<HttpClientHandler, HttpClient, HttpRequestMessage>? reqAction = null, Action<HttpClientHandler, HttpClient, HttpRequestMessage, HttpResponseMessage>? respAction = null)
{
var resp = string.Empty;
await HttpWebReqRespAsync(new Uri(url), HttpMethod.Post,
(reqAction != null) ? reqAction : (clientHandler, client, requestMessage) =>
{
requestMessage.Content = new StringContent(postFieldData, System.Text.Encoding.UTF8, System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded;charset=UTF-8")) { };
},
(respAction != null) ? respAction : async (clientHandler, client, requestMessage, responseMessage) =>
{
resp = await responseMessage.Content.ReadAsStringAsync();
}
);
return resp;
}
private static void HttpWebReqResp(Uri uri, HttpMethod httpMethod, Action<HttpClientHandler, HttpClient, HttpRequestMessage>? reqAction = null, Action<HttpClientHandler, HttpClient, HttpRequestMessage, HttpResponseMessage>? respAction = null)
{
var httpClientHandler = new HttpClientHandler()
{
AutomaticDecompression = System.Net.DecompressionMethods.All,
SslProtocols = System.Security.Authentication.SslProtocols.None,
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; },
};
var httpClient = new HttpClient(httpClientHandler)
{
Timeout = TimeSpan.FromMilliseconds(60000.0),
};
var httpRequestMessage = new HttpRequestMessage()
{
RequestUri = uri,
Method = httpMethod,
};
if (httpClient.DefaultRequestHeaders.UserAgent.Count <= 0)
{
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36");
}
if (reqAction != null)
{
reqAction(httpClientHandler, httpClient, httpRequestMessage);
}
var httpResponseMessage = httpClient.Send(httpRequestMessage);
if (respAction != null)
{
respAction(httpClientHandler, httpClient, httpRequestMessage, httpResponseMessage);
}
}
private static async Task HttpWebReqRespAsync(Uri uri, HttpMethod httpMethod, Action<HttpClientHandler, HttpClient, HttpRequestMessage>? reqAction = null, Action<HttpClientHandler, HttpClient, HttpRequestMessage, HttpResponseMessage>? respAction = null)
{
var httpClientHandler = new HttpClientHandler()
{
AutomaticDecompression = System.Net.DecompressionMethods.All,
SslProtocols = System.Security.Authentication.SslProtocols.None,
ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; },
};
var httpClient = new HttpClient(httpClientHandler)
{
Timeout = TimeSpan.FromMilliseconds(60000.0),
};
var httpRequestMessage = new HttpRequestMessage()
{
RequestUri = uri,
Method = httpMethod,
};
if (httpClient.DefaultRequestHeaders.UserAgent.Count <= 0)
{
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36");
}
if (reqAction != null)
{
reqAction(httpClientHandler, httpClient, httpRequestMessage);
}
var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage);
if (respAction != null)
{
respAction(httpClientHandler, httpClient, httpRequestMessage, httpResponseMessage);
}
}
}