using DotNet4.Utilities;
using ryCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
namespace RyWeb
{
///
///
///
public class QuickWeb
{
///
///
///
public QuickWeb()
{
}
///
///
///
/// 超时时间
public QuickWeb(int Timeout)
{
this.Timeout= Timeout;
}
///
/// 以post方式获取网页源码
///
///
///
///
///
public HttpResult Post(string url, string post, string cookie)
{
return Post(url, post, "application/x-www-form-urlencoded", cookie);
}
///
/// 以post方式提交json内容
///
///
///
///
///
public HttpResult PostJson(string url, string post, string cookie)
{
return Post(url, post, "application/json", cookie);
}
///
/// 以post方式提交json内容
///
///
///
///
public HttpResult PostJson(string url, string post)
{
return Post(url, post, "application/json", "");
}
///
/// 以post方式获取网页源码
///
///
///
///
///
///
public HttpResult Post(string url, string post,string ContentType, string cookie)
{
try
{
HttpHelper t = new HttpHelper();
HttpItem m = new HttpItem()
{
URL = url,
Postdata = post,
ContentType = ContentType,
Method = "POST",
Timeout = Timeout,
ReadWriteTimeout = Timeout,
UserAgent = UserAgent,
Referer=Referer,
KeepAlive = false,
ProxyIp= ProxyIp,
Allowautoredirect= AllowAutoRedirect
};
if (cookie!=null && cookie.Length > 0)
{
m.Cookie = cookie;
}
HttpResult r = t.GetHtml(m);
return r;
}
catch
{
HttpResult r = new HttpResult();
return r;
}
}
///
/// 以post方式获取网页源码
///
///
///
///
public HttpResult Post(string url, string post)
{
return Post(url, post, cookie);
}
///
/// 获取网址对应的文件大小
///
///
///
public long GetSize(string url)
{
return GetSize(url, out _);
}
///
/// 获取网址对应的文件大小
///
///
///
///
public long GetSize(string url,out string errormsg)
{
errormsg = "";
try
{
HttpHelper t = new HttpHelper();
HttpItem m = new HttpItem()
{
URL = url,
Method = "HEAD",
Allowautoredirect = AllowAutoRedirect,
Cookie = cookie,
Timeout = Timeout,
ReadWriteTimeout = Timeout,
UserAgent = UserAgent,
Referer = Referer,
KeepAlive=false,
ProxyIp = ProxyIp
};
HttpResult r = t.GetHtml(m);
if (r.StatusCode == System.Net.HttpStatusCode.OK)
{
return GetContentLen();
}
else if (r.StatusCode == System.Net.HttpStatusCode.NotFound)
{
errormsg = "404错误";
return -404;
}
else if (r.StatusCode == System.Net.HttpStatusCode.Found)
{
return GetContentLen();
}
else if (r.StatusCode == 0)
{
errormsg = r.Html;
return 0;
}
else if (r.StatusCode == System.Net.HttpStatusCode.MethodNotAllowed)
{
System.Net.ServicePointManager.DefaultConnectionLimit = 512;
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
long totalBytes = response.ContentLength;
return totalBytes;
}
else
return 0;
long GetContentLen()
{
var size = r.Header["Content-Length"].ToInt64(0);
if (size == 0)
{
System.Net.ServicePointManager.DefaultConnectionLimit = 512;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.AllowAutoRedirect = AllowAutoRedirect;
request.Timeout = 3000;
request.ReadWriteTimeout = 3000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
long totalBytes = response.ContentLength;
return totalBytes;
}
else
{
return size;
}
}
}
catch(Exception ex)
{
errormsg = ex.Message;
return 0;
}
}
///
/// 代理
///
public string ProxyIp { get; set; }
///
///
///
public string UserAgent
{
get; set;
} = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
///
/// 是否自动跳转
///
public bool AllowAutoRedirect
{
get; set;
} = true;
///
/// 获取网页源码
///
///
///
///
public HttpResult Get(string url, string cookie)
{
try
{
HttpHelper t = new HttpHelper();
HttpItem m = new HttpItem()
{
URL = url,
ContentType= "application/x-www-form-urlencoded",
KeepAlive = false
};
if (cookie != null && cookie.Length>0)
{
m.Cookie = cookie;
}
m.UserAgent = UserAgent;
m.Allowautoredirect = AllowAutoRedirect;
m.Timeout = Timeout;
m.ReadWriteTimeout = Timeout;
m.Referer = Referer;
m.ProxyIp = ProxyIp;
HttpResult r = t.GetHtml(m);
return r;
}
catch
{
HttpResult r = new HttpResult()
{
Html = ""
};
return r;
}
}
///
/// 获取网页源码
///
///
///
///
///
public HttpResult Get(string url,Encoding encoding, string cookie)
{
try
{
HttpHelper t = new HttpHelper();
HttpItem m = new HttpItem()
{
URL = url,
KeepAlive = false
};
if (cookie != "")
{
m.Cookie = cookie;
}
m.UserAgent = UserAgent;
m.Encoding = encoding;
m.Allowautoredirect = AllowAutoRedirect;
m.Timeout = Timeout;
m.ReadWriteTimeout = Timeout;
m.Referer = Referer;
m.ProxyIp = ProxyIp;
HttpResult r = t.GetHtml(m);
return r;
}
catch
{
HttpResult r = new HttpResult()
{
Html = ""
};
return r;
}
}
///
/// 获取网页源码
///
///
///
public HttpResult Get(string url)
{
return Get(url, cookie);
}
///
///
///
public string Cookie
{
get { return cookie; }
}
///
///
///
public string Referer
{
get;set;
}
///
///
///
public int Timeout { get; set; } = 10000;
///
///
///
///
///
///
public string UrlEncode(string str, Encoding encode)
{
StringBuilder sb = new StringBuilder();
byte[] byStr = encode.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
for (int i = 0; i < byStr.Length; i++)
{
var item = Convert.ToString(byStr[i], 16);
if (item.Length == 1) { item = "0" + item; }
sb.Append(@"%" + item);
}
return (sb.ToString());
}
///
///
///
///
///
public string UrlEncode(string str)
{
return UrlEncode(str, Encoding.UTF8);
}
private readonly string cookie = "";
///
/// 将相对网址转换成绝对网址
///
/// 相对网址
/// 当前页面地址
/// 转换后的绝对网址
public string ConvertUrl(string rel_url, string cur_pageUrl)
{
if (rel_url == "")
{
return cur_pageUrl;
}
try
{
string _rel_url = rel_url;
if (_rel_url.IndexOf("//")==0)
{
int iPos = cur_pageUrl.IndexOf(":");
if (iPos > 0)
{
_rel_url = cur_pageUrl.Substring(0,iPos)+ ":" + _rel_url;
}
}
Uri baseUri = new Uri(cur_pageUrl); //
Uri absoluteUri = new Uri(baseUri, _rel_url);//相对绝对路径都在这里转 这里的urlx ="../test.html"
return absoluteUri.AbsoluteUri.Replace("&", "&");//
}
catch { return rel_url; }
}
}
}