在.net使用代理浏览网页

发表于:2007-06-11来源:作者:点击数: 标签:
不需要使用用户名密码的代理 服务器 ,匿名代理 %@ 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.co

不需要使用用户名密码的代理服务器,匿名代理

<%@ 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://www.ltesting.net

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
...