×

在.net使用代理浏览网页

hqy hqy 发表于2021-06-25 10:08:26 浏览1263 评论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>


打赏

本文链接:https://www.kinber.cn/post/1771.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客