📄 webfunc.cs
字号:
using System;
using System.Net;
using System.Windows.Forms;
using feiyun0112.cnblogs.com.CSDNReader.Controls;
using feiyun0112.cnblogs.com.CSDNReader.Model;
namespace feiyun0112.cnblogs.com.CSDNReader.Functions
{
/// <summary>
/// Summary description for WEBFunc.
/// </summary>
public class WEBFunc
{
private string _Html;
public WEBFunc()
{
//
// TODO: Add constructor logic here
//
}
private CookieCollection _Cookies;
/// <summary>
/// 从服务端返回的cookie
/// </summary>
public CookieCollection Cookies
{
get { return _Cookies; }
set { _Cookies = value; }
}
private CookieContainer _ReqCookies=new CookieContainer();
public CookieContainer ReqCookies
{
get {
if (_ReqCookies == null)
{
_ReqCookies = new CookieContainer();
}
return _ReqCookies;
}
set { _ReqCookies = value; }
}
private string _Referer="";
public string Referer
{
get { return _Referer; }
set { _Referer = value; }
}
public string GetHTML(string URL)
{
return GetHTML(URL, "",null);
}
public string GetHTML(string URL, System.Text.Encoding encoding)
{
return GetHTML(URL,"",encoding );
}
/// <summary>
/// 获取指定地址的html
/// </summary>
/// <param name="URL"></param>
/// <param name="PostData"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public string GetHTML(string URL, string PostData, System.Text.Encoding encoding)
{
try
{
Variant.Err = "";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
if (this.Referer == "")
{
request.Referer = "http://community.csdn.net/";
}
else
{
request.Referer = this.Referer;
}
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";
request.CookieContainer = this.ReqCookies;
//设置代理
if(Setting.UseProxy)
{
if(!Setting.IEProxy)
{
WebProxy proxy = new WebProxy();
try
{
proxy.Address = new Uri(String.Format("http://{0}:{1}", Setting.ProxyServer, Setting.ProxyPort));
if (Setting.ProxyUsername.Length > 0 && Setting.ProxyPassword.Length > 0)
{
proxy.Credentials = new NetworkCredential(Setting.ProxyUsername, Setting.ProxyPassword);
}
request.Proxy=proxy;
}
catch
{
}
}
}
else
{
request.Proxy = null;
}
//request.Timeout = 30000;
if (encoding == null)
encoding = Variant.Encoding;
//设置cookie
if (Variant.CurrentUser != null)
{
CookieCollection tmpCookies = Variant.CurrentUser.Cookies;
System.Uri uri = new Uri(URL);
foreach (System.Net.Cookie cookie in tmpCookies)
{
cookie.Domain = uri.Host;
}
request.CookieContainer.Add(tmpCookies);
}
//提交的数据
if (PostData != null && PostData.Length > 0)
{
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
byte[] b = encoding.GetBytes(PostData);
request.ContentLength = b.Length;
using (System.IO.Stream sw = request.GetRequestStream())
{
try
{
sw.Write(b, 0, b.Length);
}
catch
{
Variant.Err = "KeepAliveFailure";
_Html = "";
return _Html;
}
finally
{
if (sw != null) { sw.Close(); }
}
}
}
HttpWebResponse response = null;
System.IO.StreamReader sr = null;
try
{
response = (HttpWebResponse)request.GetResponse();
this.Cookies = response.Cookies;
sr = new System.IO.StreamReader(response.GetResponseStream(), encoding);
_Html = sr.ReadToEnd();
}
catch (WebException webex)
{
if (webex.Status == WebExceptionStatus.KeepAliveFailure)
{
_Html = "";
Variant.Err = "KeepAliveFailure";
return _Html;
}
else
{
_Html = "";
Variant.Err = webex.Message;
return _Html;
}
}
catch (System.Exception ex)
{
_Html = "";
Variant.Err = ex.Message;
return _Html;
}
finally
{
if (sr != null) { sr.Close(); }
if (response != null) { response.Close(); }
response = null;
request = null;
}
return _Html;
}
catch
{
return "";
}
}
/// <summary>
/// 获得图片
/// </summary>
/// <param name="URL"></param>
/// <returns></returns>
internal System.Drawing.Image GetImg(string URL)
{
System.Drawing.Image img = null;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
request.Accept = "*/*";
request.Referer = "http://community.csdn.net/";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Maxthon)";
request.CookieContainer = this.ReqCookies;
//设置代理
if (Setting.UseProxy)
{
if (!Setting.IEProxy)
{
WebProxy proxy = new WebProxy();
try
{
proxy.Address = new Uri(String.Format("http://{0}:{1}", Setting.ProxyServer, Setting.ProxyPort));
if (Setting.ProxyUsername != "" && Setting.ProxyPassword != "")
{
proxy.Credentials = new NetworkCredential(Setting.ProxyUsername, Setting.ProxyPassword);
}
request.Proxy = proxy;
}
catch
{
}
}
}
else
{
request.Proxy = null;
}
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
img = System.Drawing.Image.FromStream(response.GetResponseStream());
}
catch
{
img = null;
}
finally
{
if (response != null) { response.Close(); }
response = null;
request = null;
}
return img;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -