云收藏 增加AI检测图片
This commit is contained in:
1 parent
b28940c02d
commit
b54a5f5310
36 files changed
+1978
-523
No files matched your search
+172
-156
@@ -8,6 +8,7 @@ using System.Net.Security;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CsharpHttpHelper
|
||||
@@ -52,6 +53,16 @@ namespace CsharpHttpHelper
|
||||
});
|
||||
}
|
||||
|
||||
public static Task<HttpResult> GetAsync(string Url)
|
||||
{
|
||||
return RequestAsync(new HttpItem { URL = Url, Method = MethodType.Get });
|
||||
}
|
||||
|
||||
public static Task<HttpResult> PostAsync(string Url, string PostData)
|
||||
{
|
||||
return RequestAsync(new HttpItem { URL = Url, Method = MethodType.Post, Postdata = PostData });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载文件
|
||||
/// </summary>
|
||||
@@ -77,17 +88,13 @@ namespace CsharpHttpHelper
|
||||
|
||||
public static HttpResult Request(HttpItem item)
|
||||
{
|
||||
HttpResult result = new HttpHelper().GetHtml(item);
|
||||
if (!string.IsNullOrEmpty(result.Html))
|
||||
{
|
||||
if (result.Html[0] == 65279)
|
||||
{
|
||||
result.Html = result.Html.Remove(0, 1);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return new HttpHelper().GetHtml(item);
|
||||
}
|
||||
|
||||
public static Task<HttpResult> RequestAsync(HttpItem item)
|
||||
{
|
||||
return new HttpHelper().GetHtmlCoreAsync(item);
|
||||
}
|
||||
|
||||
#region Cookie
|
||||
/// <summary>
|
||||
@@ -137,8 +144,6 @@ namespace CsharpHttpHelper
|
||||
private Encoding postencoding = Encoding.Default;
|
||||
//HttpWebRequest对象用来发起请求
|
||||
private HttpWebRequest request = null;
|
||||
//获取影响流的数据对象
|
||||
private HttpWebResponse response = null;
|
||||
/// <summary>
|
||||
/// 根据相传入的数据,得到相应页面数据
|
||||
/// </summary>
|
||||
@@ -146,112 +151,49 @@ namespace CsharpHttpHelper
|
||||
/// <returns>返回HttpResult类型</returns>
|
||||
public HttpResult GetHtml(HttpItem item)
|
||||
{
|
||||
//返回参数
|
||||
HttpResult result = new HttpResult();
|
||||
result.item = item;
|
||||
try
|
||||
{
|
||||
//准备参数
|
||||
SetRequest(item);
|
||||
if (item.Method == MethodType.Post || item.Method == MethodType.Put)
|
||||
{
|
||||
byte[] body;
|
||||
if (item.PostDataType == PostDataType.FilePath && !string.IsNullOrEmpty(item.Postdata))
|
||||
{
|
||||
using var reader = new StreamReader(item.Postdata, postencoding);
|
||||
body = postencoding.GetBytes(reader.ReadToEnd());
|
||||
}
|
||||
else body = GetPostBytes(item);
|
||||
if (body != null)
|
||||
{
|
||||
request.ContentLength = body.Length;
|
||||
using var stream = request.GetRequestStream();
|
||||
stream.Write(body, 0, body.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result = new HttpResult();
|
||||
result.Cookie = string.Empty;
|
||||
result.Header = null;
|
||||
result.Html = ex.Message;
|
||||
result.StatusDescription = "配置参数时出错:" + ex.Message;
|
||||
return result;
|
||||
return ConfigurationError(ex);
|
||||
}
|
||||
|
||||
var result = new HttpResult { item = item };
|
||||
try
|
||||
{
|
||||
#region 得到请求的response
|
||||
using (response = (HttpWebResponse)request.GetResponse())
|
||||
{
|
||||
result.StatusCode = response.StatusCode;
|
||||
result.StatusDescription = response.StatusDescription;
|
||||
result.Header = response.Headers;
|
||||
if (response.Cookies != null) result.CookieCollection = response.Cookies;
|
||||
if (response.Headers["set-cookie"] != null) result.Cookie = response.Headers["set-cookie"];
|
||||
MemoryStream _stream = new MemoryStream();
|
||||
//GZIIP处理
|
||||
if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
//开始读取流并设置编码方式
|
||||
//new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);
|
||||
//.net4.0以下写法
|
||||
_stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//开始读取流并设置编码方式
|
||||
//response.GetResponseStream().CopyTo(_stream, 10240);
|
||||
//.net4.0以下写法
|
||||
_stream = GetMemoryStream(response.GetResponseStream());
|
||||
}
|
||||
//获取Byte
|
||||
byte[] ResponseByte = _stream.ToArray();
|
||||
_stream.Close();
|
||||
if (ResponseByte != null & ResponseByte.Length > 0)
|
||||
{
|
||||
//是否返回Byte类型数据
|
||||
if (item.ResultType == ResultType.Byte) result.ResultByte = ResponseByte;
|
||||
//从这里开始我们要无视编码了
|
||||
if (encoding == null)
|
||||
{
|
||||
Match meta = Regex.Match(Encoding.Default.GetString(ResponseByte), "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
|
||||
string c = (meta.Groups.Count > 1) ? meta.Groups[2].Value.ToLower().Trim() : string.Empty;
|
||||
if (c.Length > 2)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (c.IndexOf(" ") > 0) c = c.Substring(0, c.IndexOf(" "));
|
||||
encoding = Encoding.GetEncoding(c.Replace("\"", "").Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk").Trim());
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (string.IsNullOrEmpty(response.CharacterSet)) encoding = Encoding.UTF8;
|
||||
else encoding = Encoding.GetEncoding(response.CharacterSet);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (string.IsNullOrEmpty(response.CharacterSet)) encoding = Encoding.UTF8;
|
||||
else encoding = Encoding.GetEncoding(response.CharacterSet);
|
||||
}
|
||||
}
|
||||
//得到返回的HTML
|
||||
result.Html = encoding.GetString(ResponseByte);
|
||||
}
|
||||
else
|
||||
{
|
||||
//得到返回的HTML
|
||||
result.Html = "本次请求并未返回任何数据";
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
using var response = (HttpWebResponse)request.GetResponse();
|
||||
SetResponseHeaders(result, response);
|
||||
if (response.Cookies != null) result.CookieCollection = response.Cookies;
|
||||
result.Cookie = response.Headers["set-cookie"];
|
||||
using var source = OpenResponseStream(response);
|
||||
using var buffer = new MemoryStream();
|
||||
source.CopyTo(buffer);
|
||||
DecodeBody(item, result, response, buffer.ToArray());
|
||||
}
|
||||
catch (WebException ex)
|
||||
catch (WebException ex) when (ex.Response is HttpWebResponse)
|
||||
{
|
||||
//这里是在发生异常时返回的错误信息
|
||||
response = (HttpWebResponse)ex.Response;
|
||||
result.Html = ex.Message;
|
||||
if (response != null)
|
||||
{
|
||||
using (response)
|
||||
{
|
||||
result.StatusCode = response.StatusCode;
|
||||
result.StatusDescription = response.StatusDescription;
|
||||
result.Header = response.Headers;
|
||||
// 保留错误响应正文,供微信调用方识别业务错误和登录失效。
|
||||
using (var source = response.GetResponseStream())
|
||||
using (var decoded = string.Equals(response.ContentEncoding, "gzip", StringComparison.OrdinalIgnoreCase)
|
||||
? (Stream)new GZipStream(source, CompressionMode.Decompress) : source)
|
||||
using (var reader = new StreamReader(decoded, item.Encoding ?? Encoding.UTF8))
|
||||
result.Html = reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
using var response = (HttpWebResponse)ex.Response;
|
||||
SetResponseHeaders(result, response);
|
||||
using var reader = new StreamReader(OpenResponseStream(response), item.Encoding ?? Encoding.UTF8);
|
||||
result.Html = reader.ReadToEnd();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -260,22 +202,127 @@ namespace CsharpHttpHelper
|
||||
if (item.IsToLower) result.Html = result.Html.ToLower();
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 4.0以下.net版本取数据使用
|
||||
/// </summary>
|
||||
/// <param name="streamResponse">流</param>
|
||||
private MemoryStream GetMemoryStream(Stream streamResponse)
|
||||
|
||||
public Task<HttpResult> GetHtmlAsync(HttpItem item)
|
||||
{
|
||||
MemoryStream _stream = new MemoryStream();
|
||||
int Length = 256;
|
||||
Byte[] buffer = new Byte[Length];
|
||||
int bytesRead = streamResponse.Read(buffer, 0, Length);
|
||||
while (bytesRead > 0)
|
||||
// 每次异步调用独立保存请求和编码,允许同一实例并发使用。
|
||||
return new HttpHelper().GetHtmlCoreAsync(item);
|
||||
}
|
||||
|
||||
private async Task<HttpResult> GetHtmlCoreAsync(HttpItem item)
|
||||
{
|
||||
using var timeout = new CancellationTokenSource();
|
||||
using var abort = timeout.Token.Register(() => request.Abort());
|
||||
try
|
||||
{
|
||||
_stream.Write(buffer, 0, bytesRead);
|
||||
bytesRead = streamResponse.Read(buffer, 0, Length);
|
||||
SetRequest(item);
|
||||
timeout.CancelAfter(request.Timeout);
|
||||
if (item.Method == MethodType.Post || item.Method == MethodType.Put)
|
||||
{
|
||||
byte[] body;
|
||||
if (item.PostDataType == PostDataType.FilePath && !string.IsNullOrEmpty(item.Postdata))
|
||||
{
|
||||
using var reader = new StreamReader(item.Postdata, postencoding);
|
||||
body = postencoding.GetBytes(await reader.ReadToEndAsync().ConfigureAwait(false));
|
||||
}
|
||||
else body = GetPostBytes(item);
|
||||
if (body != null)
|
||||
{
|
||||
request.ContentLength = body.Length;
|
||||
using var stream = await request.GetRequestStreamAsync().ConfigureAwait(false);
|
||||
await stream.WriteAsync(body, 0, body.Length).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return ConfigurationError(ex);
|
||||
}
|
||||
|
||||
var result = new HttpResult { item = item };
|
||||
try
|
||||
{
|
||||
using var response = (HttpWebResponse)await request.GetResponseAsync().ConfigureAwait(false);
|
||||
SetResponseHeaders(result, response);
|
||||
if (response.Cookies != null) result.CookieCollection = response.Cookies;
|
||||
result.Cookie = response.Headers["set-cookie"];
|
||||
using var source = OpenResponseStream(response);
|
||||
using var buffer = new MemoryStream();
|
||||
await source.CopyToAsync(buffer).ConfigureAwait(false);
|
||||
DecodeBody(item, result, response, buffer.ToArray());
|
||||
}
|
||||
catch (WebException ex) when (ex.Response is HttpWebResponse)
|
||||
{
|
||||
using var response = (HttpWebResponse)ex.Response;
|
||||
SetResponseHeaders(result, response);
|
||||
using var reader = new StreamReader(OpenResponseStream(response), item.Encoding ?? Encoding.UTF8);
|
||||
result.Html = await reader.ReadToEndAsync().ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.Html = ex.Message;
|
||||
}
|
||||
if (item.IsToLower) result.Html = result.Html.ToLower();
|
||||
return result;
|
||||
}
|
||||
|
||||
private static HttpResult ConfigurationError(Exception ex)
|
||||
{
|
||||
return new HttpResult { Cookie = string.Empty, Html = ex.Message, StatusDescription = "配置参数时出错:" + ex.Message };
|
||||
}
|
||||
|
||||
private static void SetResponseHeaders(HttpResult result, HttpWebResponse response)
|
||||
{
|
||||
result.StatusCode = response.StatusCode;
|
||||
result.StatusDescription = response.StatusDescription;
|
||||
result.Header = response.Headers;
|
||||
}
|
||||
|
||||
private static Stream OpenResponseStream(HttpWebResponse response)
|
||||
{
|
||||
var stream = response.GetResponseStream();
|
||||
return string.Equals(response.ContentEncoding, "gzip", StringComparison.OrdinalIgnoreCase)
|
||||
? new GZipStream(stream, CompressionMode.Decompress) : stream;
|
||||
}
|
||||
|
||||
private void DecodeBody(HttpItem item, HttpResult result, HttpWebResponse response, byte[] bytes)
|
||||
{
|
||||
if (bytes.Length > 0)
|
||||
{
|
||||
//是否返回Byte类型数据
|
||||
if (item.ResultType == ResultType.Byte) result.ResultByte = bytes;
|
||||
//从这里开始我们要无视编码了
|
||||
if (encoding == null)
|
||||
{
|
||||
Match meta = Regex.Match(Encoding.Default.GetString(bytes), "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase);
|
||||
string c = (meta.Groups.Count > 1) ? meta.Groups[2].Value.ToLower().Trim() : string.Empty;
|
||||
if (c.Length > 2)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (c.IndexOf(" ") > 0) c = c.Substring(0, c.IndexOf(" "));
|
||||
encoding = Encoding.GetEncoding(c.Replace("\"", "").Replace("'", "").Replace(";", "").Replace("iso-8859-1", "gbk").Trim());
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (string.IsNullOrEmpty(response.CharacterSet)) encoding = Encoding.UTF8;
|
||||
else encoding = Encoding.GetEncoding(response.CharacterSet);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (string.IsNullOrEmpty(response.CharacterSet)) encoding = Encoding.UTF8;
|
||||
else encoding = Encoding.GetEncoding(response.CharacterSet);
|
||||
}
|
||||
}
|
||||
//得到返回的HTML
|
||||
result.Html = encoding.GetString(bytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
//得到返回的HTML
|
||||
result.Html = "本次请求并未返回任何数据";
|
||||
}
|
||||
return _stream;
|
||||
}
|
||||
/// <summary>
|
||||
/// 为请求准备参数
|
||||
@@ -308,6 +355,7 @@ namespace CsharpHttpHelper
|
||||
request.UserAgent = item.UserAgent;
|
||||
// 编码
|
||||
encoding = item.Encoding;
|
||||
if (item.Method != MethodType.Get && item.PostEncoding != null) postencoding = item.PostEncoding;
|
||||
//设置安全凭证
|
||||
request.Credentials = item.ICredentials;
|
||||
//设置Cookie
|
||||
@@ -316,8 +364,6 @@ namespace CsharpHttpHelper
|
||||
request.Referer = item.Referer;
|
||||
//是否执行跳转功能
|
||||
request.AllowAutoRedirect = item.Allowautoredirect;
|
||||
//设置Post数据
|
||||
SetPostData(item);
|
||||
//设置最大连接
|
||||
if (item.Connectionlimit > 0) request.ServicePoint.ConnectionLimit = item.Connectionlimit;
|
||||
}
|
||||
@@ -373,43 +419,13 @@ namespace CsharpHttpHelper
|
||||
request.CookieContainer.Add(item.CookieCollection);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 设置Post数据
|
||||
/// </summary>
|
||||
/// <param name="item">Http参数</param>
|
||||
private void SetPostData(HttpItem item)
|
||||
private byte[] GetPostBytes(HttpItem item)
|
||||
{
|
||||
//验证在得到结果时是否有传入数据
|
||||
if (request.Method.Trim().ToLower().Contains("post") || request.Method.Trim().ToLower().Contains("put"))
|
||||
{
|
||||
if (item.PostEncoding != null)
|
||||
{
|
||||
postencoding = item.PostEncoding;
|
||||
}
|
||||
byte[] buffer = null;
|
||||
//写入Byte类型
|
||||
if (item.PostDataType == PostDataType.Byte && item.PostdataByte != null && item.PostdataByte.Length > 0)
|
||||
{
|
||||
//验证在得到结果时是否有传入数据
|
||||
buffer = item.PostdataByte;
|
||||
}//写入文件
|
||||
else if (item.PostDataType == PostDataType.FilePath && !string.IsNullOrEmpty(item.Postdata))
|
||||
{
|
||||
StreamReader r = new StreamReader(item.Postdata, postencoding);
|
||||
buffer = postencoding.GetBytes(r.ReadToEnd());
|
||||
r.Close();
|
||||
} //写入字符串
|
||||
else if (!string.IsNullOrEmpty(item.Postdata))
|
||||
{
|
||||
buffer = postencoding.GetBytes(item.Postdata);
|
||||
}
|
||||
if (buffer != null)
|
||||
{
|
||||
request.ContentLength = buffer.Length;
|
||||
request.GetRequestStream().Write(buffer, 0, buffer.Length);
|
||||
}
|
||||
}
|
||||
if (item.PostDataType == PostDataType.Byte && item.PostdataByte?.Length > 0)
|
||||
return item.PostdataByte;
|
||||
return string.IsNullOrEmpty(item.Postdata) ? null : postencoding.GetBytes(item.Postdata);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置代理
|
||||
/// </summary>
|
||||
|
||||
Reference in new issue
Block a user