using System;
using System.IO;
using System.Net;
using System.Windows.Forms;
using System.Diagnostics;
namespace ProxyBrowser
{
public class WebBrowserEx : WebBrowser
{
private string prevPage = null;
public WebBrowserEx(): base()
{
Initialize();
Document.Click += new HtmlElementEventHandler(Document_Click);
}
private void Document_Click(object sender, HtmlElementEventArgs e)
{
string text = null;
string link = null;
HtmlElement element = this.Document.GetElementFromPoint(e.MousePosition);
if (element.TagName == "a" || element.TagName == "A")
{
text = element.OuterText;
link = element.GetAttribute("href");
}
else if (element.Parent != null)
{
text = element.Parent.OuterText;
link = element.Parent.GetAttribute("href");
}
if (link != null && link != "")
{
try
{
MessageBox.Show(text + "\n" + link);
e.ReturnValue = false;
}
catch
{
}
}
}
private void Initialize()
{
UserAgent = null;
Proxy = null;
Cookie = null;
Timeout = 5000;
}
public new void Navigate(string url)
{
string buffer = null; // バッファ
HttpWebRequest req = null;
HttpWebResponse res = null;
#region リクエストの作成
try
{
/* リクエストの設定 */
req = (HttpWebRequest)WebRequest.Create(url);
// UserAgent
if (this.UserAgent != null)
req.UserAgent = UserAgent;
// Proxy
if(this.Proxy != null)
req.Proxy = new WebProxy(this.Proxy);
// Cookie
if (this.Cookie != null)
{
req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(this.Cookie);
}
// Timeout
if (this.Timeout != 5000)
req.Timeout = this.Timeout;
}
catch (UriFormatException e)
{
Debug.WriteLine(e.ToString());
return;
}
catch (NotSupportedException e)
{
Debug.WriteLine(e.ToString());
return;
}
catch (ArgumentException e)
{
Debug.WriteLine(e.ToString());
return;
}
catch (System.Security.SecurityException e)
{
Debug.WriteLine(e.ToString());
return;
}
#endregion
try
{
res = (HttpWebResponse)req.GetResponse();
Stream st = res.GetResponseStream();
using (StreamReader sr = new StreamReader(st))
buffer = sr.ReadToEnd();
st.Close();
}
catch (WebException e)
{
Debug.WriteLine(e);
return;
}
catch (ProtocolViolationException e)
{
Debug.WriteLine(e);
return;
}
catch (InvalidOperationException e)
{
Debug.WriteLine(e);
return;
}
}
public string UserAgent
{ get; set; }
public string Proxy
{ get; set; }
public CookieCollection Cookie
{ get; set; }
public int Timeout
{ get; set; }
}
}