首页 新闻 会员 周边

c#如何实现通过http代理访问网络

0
悬赏园豆:50 [已解决问题] 解决于 2008-05-07 23:17
<P>本机通过代理访问网络,用c#编程如何实现</P> <P>期待高手</P>
DH.伊利丹的主页 DH.伊利丹 | 初学一级 | 园豆:100
提问于:2008-04-29 00:43
< >
分享
最佳答案
0
遇到问题查MSDN和google,这样解决得更快。 下面的代码来自MSDN Library: // Create a new request to the mentioned URL. HttpWebRequest myWebRequest=(HttpWebRequest)WebRequest.Create("http://www.microsoft.com"); WebProxy myProxy=new WebProxy(); // Obtain the 'Proxy' of the Default browser. myProxy=(WebProxy)myWebRequest.Proxy; // Print the Proxy Url to the console. Console.WriteLine("\nThe actual default Proxy settings are {0}",myProxy.Address); try { Console.WriteLine("\nPlease enter the new Proxy Address that is to be set:"); Console.WriteLine("(Example:http://myproxy.example.com:port)"); string proxyAddress; proxyAddress =Console.ReadLine(); if(proxyAddress.Length>0) { Console.WriteLine("\nPlease enter the Credentials "); Console.WriteLine("Username:"); string username; username =Console.ReadLine(); Console.WriteLine("\nPassword:"); string password; password =Console.ReadLine(); // Create a new Uri object. Uri newUri=new Uri(proxyAddress); // Associate the newUri object to 'myProxy' object so that new myProxy settings can be set. myProxy.Address=newUri; // Create a NetworkCredential object and associate it with the Proxy property of request object. myProxy.Credentials=new NetworkCredential(username,password); myWebRequest.Proxy=myProxy; } Console.WriteLine("\nThe Address of the new Proxy settings are {0}",myProxy.Address); HttpWebResponse myWebResponse=(HttpWebResponse)myWebRequest.GetResponse(); //---------下面几行是我加的------ StreamReader reader= new StreamReader(myWebResponse.GetResponseStream()); string html = reader.ReadToEnd(); Console.WriteLine(html);
deerchao | 大侠五级 |园豆:8367 | 2008-04-29 12:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册