在.net使用代理浏览网页

类别:.NET开发 点击:0 评论:0 推荐:
不需要使用用户名密码的代理服务器,匿名代理
<%@ 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>

本文地址:http://com.8s8s.com/it/it42149.htm