首页 > 编程 > .NET > 正文

在.net使用代理浏览网页

2024-07-10 12:59:14
字体:
来源:转载
供稿:网友

最大的网站源码资源下载站,

不需要使用用户名密码的代理服务器,匿名代理
<%@ 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>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表