using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace WebBrowserDemo
{
public partial class _Default : System.Web.UI.Page
{
#region Events
protected void Page_Load(object sender, EventArgs e)
{
}
void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
////if (wb.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
////{ return; }
////mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)wb.Document;
////string source = doc.body.outerHTML;// 网页源码
////lbResult.Text = source;
}
protected void btnRequest_Click(object sender, EventArgs e)
{
//lbResult.Text = GetURLContent(txtUrl.Text.Trim(), "GB2312");
string url = txtUrl.Text.Trim();
System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");
string strResult = GetStringByUrl(url, encoding);
int intBody = strResult.Trim().ToLower().IndexOf("<body");
int intEndBody = strResult.ToLower().Trim().IndexOf(intBody,"<body");
string strTemp = strResult.Substring((intBody + 5),(intEndBody-intBody -5));
Control control = this.ParseControl(strTemp);
PlaceHolder1.Controls.Add(control);
}
protected void btnWc_Click(object sender, EventArgs e)
{
lbResult.Text = GetPageByWebClient(txtUrl.Text.Trim());
}
protected void btnWb_Click(object sender, EventArgs e)
{
//lbResult.Text = GetURLContentByWebBrowser(txtUrl.Text.Trim());
//System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(GetURLContentByWebBrowser));
//thread.Start();
string url = txtUrl.Text.Trim();
lbResult.Text = GetPageStringbyWebBrowser(url);
}
#endregion
#region Methods
private string RandomKey(int b, int e)
{
return DateTime.Now.ToString("yyyyMMdd-HHmmss-fff-") + this.getRandomID(b, e);
}
private int getRandomID(int minValue, int maxValue)
{
Random ri = new Random(unchecked((int)DateTime.Now.Ticks));
int k = ri.Next(minValue, maxValue);
return k;
}
private string GuidString
{
get { return Guid.NewGuid().ToString(); }
}
private string GetPageByWebClient(string url)
{
string result = null;
if (url.Equals("about:blank")) return null; ;
if (!url.StartsWith("http://") && !url.StartsWith("https://")) { url = "http://" + url; }
string filename = RandomKey(1111, 9999) + ".txt";
DownloadOneFileByURLWithWebClient(filename, url, "D:\\Record\\");
StreamReader sr = new StreamReader("D:\\Record\\" + filename, System.Text.Encoding.Default);
try { result = sr.ReadToEnd(); return result; }
catch { return null; }
finally
{
if (sr != null) { sr.Close(); }
}
}
///Web Client Method ,only For Small picture
/// </summary>
/// <param name="fileName"></param>
/// <param name="url"></param>
/// <param name="localPath"></param>
public static void DownloadOneFileByURLWithWebClient(string fileName, string url, string localPath)
{
System.Net.WebClient wc = new System.Net.WebClient();
if (File.Exists(localPath + fileName)) { File.Delete(localPath + fileName); }
if (Directory.Exists(localPath) == false) { Directory.CreateDirectory(localPath); }
wc.DownloadFile(url + fileName, localPath + fileName);
}
#region 读取页面详细信息
/// <summary> /// 读取页面详细信息
/// </summary>
///<param name="url">需要读取的地址</param>
/// <returns></returns>
//[STAThread]
public static string GetURLContentByWebBrowser(string url)
{
try
{
//webBrowser1 = new WebBrowser();
string result = null;
WebBrowser wb = new WebBrowser();
////if (wb != null)
////{ wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted); }
////if (String.IsNullOrEmpty(url)) return result;
////if (url.Equals("about:blank")) return result;
////if (!url.StartsWith("http://") && !url.StartsWith("https://"))
////{
//// url = "http://" + url;
////}
////try
////{
//// wb.Navigate(new Uri(url));
//// result = wb.DocumentText;
//// lbResult.Text = result;
////}
////catch (System.UriFormatException)
////{
//// return result;
////}
return result;
}
catch (Exception ex)
{
//WriteLog.Writelog("这是获取页面全部html代码时发生的错误:" + url, ex);
return null;
}
}
[STAThread]
public void GetURLContentByWebBrowser()
{
try
{
//webBrowser1 = new WebBrowser();
string url = txtUrl.Text.Trim();
string result = null;
WebBrowser wb = new WebBrowser();
////if (wb != null){ wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted); }
if (String.IsNullOrEmpty(url)) return;
if (url.Equals("about:blank")) return; ;
if (!url.StartsWith("http://") && !url.StartsWith("https://")) { url = "http://" + url; }
try
{
wb.Navigate(new Uri(url));
result = wb.DocumentText;
lbResult.Text = result;
}
catch (System.UriFormatException)
{ }
return;
}
catch (Exception ex)
{
//WriteLog.Writelog("这是获取页面全部html代码时发生的错误:" + url, ex);
throw ex;
//return ;
}
}
private string GetPageStringbyWebBrowser(string url)
{
if (url.Equals("about:blank")) return null; ;
if (!url.StartsWith("http://") && !url.StartsWith("https://")) { url = "http://" + url; }
WebBrowser myWB = new WebBrowser();
myWB.ScrollBarsEnabled = false;
myWB.Navigate(url);
while (myWB.ReadyState != WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
if (myWB != null)
{
System.IO.StreamReader getReader = null;
try
{
getReader = new System.IO.StreamReader(myWB.DocumentStream, System.Text.Encoding.GetEncoding(myWB.Document.Encoding));
string gethtml = getReader.ReadToEnd();
return gethtml;
}
catch { return null; }
finally
{
if (getReader != null) { getReader.Close(); }
myWB.Dispose();
}
}
return null;
}
#endregion
#region 读取页面详细信息
/// <summary> /// 读取页面详细信息
/// </summary>
///<param name="Url">需要读取的地址</param>
/// <param name="encoding">读取的编码方式</param>
/// <returns></returns>
public static string GetStringByUrl(string Url, System.Text.Encoding encoding)
{
if (Url.Equals("about:blank")) return null; ;
if (!Url.StartsWith("http://") && !Url.StartsWith("https://")) { Url = "http://" + Url; }
int dialCount = 0;
loop:
StreamReader sreader = null;
string result = string.Empty;
try
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
//httpWebRequest.Timeout = 20;
#region 关键参数,否则会取不到内容 Important Parameters,else get nothing.
httpWebRequest.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
httpWebRequest.Accept = "*/*";
httpWebRequest.KeepAlive = true;
httpWebRequest.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
#endregion
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
if (httpWebResponse.StatusCode == HttpStatusCode.OK)
{
sreader = new StreamReader(httpWebResponse.GetResponseStream(), encoding);
char[] cCont = new char[256];
int count = sreader.Read(cCont, 0, 256);
while (count > 0)
{ // Dumps the 256 characters on a string and displays the string to the console.
String str = new String(cCont, 0, count);
result += str;
count = sreader.Read(cCont, 0, 256);
}
}
if (null != httpWebResponse) { httpWebResponse.Close(); }
return result;
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ConnectFailure) { dialCount++; ReDial(); }
if (dialCount < 5) { goto loop; }
return null;
}
finally { if (sreader != null) { sreader.Close(); } }
}
#endregion
public static void ReDial()
{
int res = 1;
////while (res != 0)
////{
//// CSDNWebTest.RASDisplay ras = new RASDisplay();
//// ras.Disconnect();
//// res = ras.Connect("asdl");
//// System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10));
////}
}
#endregion
}
}