在.net使用代理浏览网页
类别: ASP.NET教程
不需要使用用户名密码的代理服务器,匿名代理
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<script language="C#" runat ="server">
void Page_Load(Object Src, EventArgs E) {
myPage.Text = readHtmlPage("http://www.iwbuy.com");
}
private String readHtmlPage(string url)
{
String result;
WebResponse objResponse;
WebProxy proxyObject = new WebProxy("http://urproxy:port/",true);
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objRequest.Proxy = proxyObject;
objResponse = objRequest.GetResponse();
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream()) )
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return result;
}
</script>
<html>
<body>
<b>下面的内容框可以显示你要浏览的网页
<a href="http://www.iwbuy.com">www.iwbuy.com</a></b><hr/>
<asp:literal id="myPage" runat ="server"/>
</body>
</html>
需使用用户名密码的代理服务器使用方法
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<script language="C#" runat ="server">
void Page_Load(Object Src, EventArgs E) {
myPage.Text = readHtmlPage("http://www.iwbuy.com");
}
private String readHtmlPage(string url)
{
String result;
HttpWebRequest makeReq =(HttpWebRequest)WebRequest.Create("http://www.iwbuy.com/");
NetworkCredential giveCred = new NetworkCredential("username","password","domain");
CredentialCache putCache = new CredentialCache();
putCache.Add(new Uri("http://www.iwbuy.com/"), "Basic", myCred);
makeReq.Credentials = myCache;
WebResponse objResponse;
objResponse = objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()) )
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return result;
}
</script>
<html>
<body>
<b>下面的内容框可以显示你要浏览的网页
<a href="http://www.iwbuy.com">www.iwbuy.com</a></b>
<hr/>
<asp:literal id="myPage" runat ="server"/>
</body>
</html>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<script language="C#" runat ="server">
void Page_Load(Object Src, EventArgs E) {
myPage.Text = readHtmlPage("http://www.iwbuy.com");
}
private String readHtmlPage(string url)
{
String result;
WebResponse objResponse;
WebProxy proxyObject = new WebProxy("http://urproxy:port/",true);
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objRequest.Proxy = proxyObject;
objResponse = objRequest.GetResponse();
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream()) )
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return result;
}
</script>
<html>
<body>
<b>下面的内容框可以显示你要浏览的网页
<a href="http://www.iwbuy.com">www.iwbuy.com</a></b><hr/>
<asp:literal id="myPage" runat ="server"/>
</body>
</html>
需使用用户名密码的代理服务器使用方法
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<script language="C#" runat ="server">
void Page_Load(Object Src, EventArgs E) {
myPage.Text = readHtmlPage("http://www.iwbuy.com");
}
private String readHtmlPage(string url)
{
String result;
HttpWebRequest makeReq =(HttpWebRequest)WebRequest.Create("http://www.iwbuy.com/");
NetworkCredential giveCred = new NetworkCredential("username","password","domain");
CredentialCache putCache = new CredentialCache();
putCache.Add(new Uri("http://www.iwbuy.com/"), "Basic", myCred);
makeReq.Credentials = myCache;
WebResponse objResponse;
objResponse = objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()) )
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return result;
}
</script>
<html>
<body>
<b>下面的内容框可以显示你要浏览的网页
<a href="http://www.iwbuy.com">www.iwbuy.com</a></b>
<hr/>
<asp:literal id="myPage" runat ="server"/>
</body>
</html>
- 上一篇: .net 有关多态性的学习笔记。
- 下一篇: 一个经典的ADO.NET入门例子
-= 资 源 教 程 =-
文 章 搜 索