------ #### ryUpdate V2.2.2101.2301 - *.[修复]修复对于指定用户更新,其它用户偶尔也能接收到更新的BUG。 #### ryControls V2.1.2101.2301 - *.[更新]ObjectListView持续汉化。 - *.[改进]ObjectListView点击单元格编辑时,编辑文本框布满整个单元格而不是布满文字区域。 - *.[改进]ObjectListView新增TopSpace属性,表示Title和Description之间的垂直间距。
283 lines
8.9 KiB
C#
283 lines
8.9 KiB
C#
using DotNet4.Utilities;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace RyWeb
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class QuickWeb
|
|
{
|
|
/// <summary>
|
|
/// 以post方式获取网页源码
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="post"></param>
|
|
/// <param name="cookie"></param>
|
|
/// <returns></returns>
|
|
public HttpResult Post(string url, string post, string cookie)
|
|
{
|
|
return Post(url, post, "application/x-www-form-urlencoded", cookie);
|
|
}
|
|
/// <summary>
|
|
/// 以post方式提交json内容
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="post"></param>
|
|
/// <param name="cookie"></param>
|
|
/// <returns></returns>
|
|
public HttpResult PostJson(string url, string post, string cookie)
|
|
{
|
|
return Post(url, post, "application/json", cookie);
|
|
}
|
|
/// <summary>
|
|
/// 以post方式提交json内容
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="post"></param>
|
|
/// <returns></returns>
|
|
public HttpResult PostJson(string url, string post)
|
|
{
|
|
return Post(url, post, "application/json", "");
|
|
}
|
|
/// <summary>
|
|
/// 以post方式获取网页源码
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="post"></param>
|
|
/// <param name="ContentType"></param>
|
|
/// <param name="cookie"></param>
|
|
/// <returns></returns>
|
|
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
|
|
};
|
|
if (cookie.Length > 0)
|
|
{
|
|
m.Cookie = cookie;
|
|
}
|
|
HttpResult r = t.GetHtml(m);
|
|
return r;
|
|
}
|
|
catch
|
|
{
|
|
HttpResult r = new HttpResult();
|
|
return r;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 以post方式获取网页源码
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="post"></param>
|
|
/// <returns></returns>
|
|
public HttpResult Post(string url, string post)
|
|
{
|
|
return Post(url, post, cookie);
|
|
}
|
|
/// <summary>
|
|
/// 获取网址对应的文件大小
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <returns></returns>
|
|
public long GetSize(string url)
|
|
{
|
|
try
|
|
{
|
|
HttpHelper t = new HttpHelper();
|
|
HttpItem m = new HttpItem()
|
|
{
|
|
URL = url,
|
|
Method = "HEAD",
|
|
Allowautoredirect = true,
|
|
Cookie = cookie,
|
|
Timeout = Timeout,
|
|
ReadWriteTimeout = Timeout,
|
|
UserAgent=UserAgent
|
|
};
|
|
HttpResult r = t.GetHtml(m);
|
|
if (r.StatusCode == System.Net.HttpStatusCode.OK)
|
|
{
|
|
return Convert.ToInt64(r.Header["Content-Length"]);
|
|
}
|
|
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;
|
|
}
|
|
catch
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string UserAgent
|
|
{
|
|
get; set;
|
|
} = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
|
|
/// <summary>
|
|
/// 获取网页源码
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="cookie"></param>
|
|
/// <returns></returns>
|
|
public HttpResult Get(string url, string cookie)
|
|
{
|
|
try
|
|
{
|
|
HttpHelper t = new HttpHelper();
|
|
HttpItem m = new HttpItem()
|
|
{
|
|
URL = url,
|
|
ContentType= "application/x-www-form-urlencoded"
|
|
};
|
|
if (cookie.Length>0)
|
|
{
|
|
m.Cookie = cookie;
|
|
}
|
|
m.UserAgent = UserAgent;
|
|
m.Allowautoredirect = true;
|
|
m.Timeout = Timeout;
|
|
m.ReadWriteTimeout = Timeout;
|
|
HttpResult r = t.GetHtml(m);
|
|
return r;
|
|
}
|
|
catch
|
|
{
|
|
HttpResult r = new HttpResult()
|
|
{
|
|
Html = ""
|
|
};
|
|
return r;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取网页源码
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
///<param name="encoding"></param>
|
|
/// <param name="cookie"></param>
|
|
/// <returns></returns>
|
|
public HttpResult Get(string url,Encoding encoding, string cookie)
|
|
{
|
|
try
|
|
{
|
|
HttpHelper t = new HttpHelper();
|
|
HttpItem m = new HttpItem()
|
|
{
|
|
URL = url
|
|
};
|
|
if (cookie != "")
|
|
{
|
|
m.Cookie = cookie;
|
|
}
|
|
m.UserAgent = UserAgent;
|
|
m.Encoding = encoding;
|
|
m.Allowautoredirect = true;
|
|
m.Timeout = Timeout;
|
|
m.ReadWriteTimeout = Timeout;
|
|
HttpResult r = t.GetHtml(m);
|
|
return r;
|
|
}
|
|
catch
|
|
{
|
|
HttpResult r = new HttpResult()
|
|
{
|
|
Html = ""
|
|
};
|
|
return r;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取网页源码
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <returns></returns>
|
|
public HttpResult Get(string url)
|
|
{
|
|
return Get(url, cookie);
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string Cookie
|
|
{
|
|
get { return cookie; }
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public int Timeout { get; set; } = 10000;
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="str"></param>
|
|
/// <param name="encode"></param>
|
|
/// <returns></returns>
|
|
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());
|
|
}
|
|
private string cookie = "";
|
|
/// <summary>
|
|
/// 将相对网址转换成绝对网址
|
|
/// </summary>
|
|
/// <param name="rel_url">相对网址</param>
|
|
/// <param name="cur_pageUrl">当前页面地址</param>
|
|
/// <returns>转换后的绝对网址</returns>
|
|
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; }
|
|
}
|
|
}
|
|
}
|