2012-12-19 267 views
5

我試圖使用公共代理服務器(http://www.unblockwebnow.info/)發送HTTP請求到目標站點使用公共代理服務器,說http://stackoverflow.com :)在HTTP客戶端

我的HTTP客戶端以下結構:

string url = "http://stackoverflow.com"; 
HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(url); 
HttpWRequest.Method = "GET"; 

WebProxy myProxy = new WebProxy(); 
myProxy.Address = new Uri("http://www.unblockwebnow.info/"); 
HttpWRequest.Proxy = myProxy; 

HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse(); 
StreamReader sr = new StreamReader(HttpWResponse.GetResponseStream(), encoding); 
var rawHTML = sr.ReadToEnd(); 
sr.Close(); 

執行代碼rawHTML後,我得到"pageok -managed by puppet - hostingcms02 pageok"

如果我註釋掉HttpWRequest.Proxy = myProxy;線,我得到了網站內容。

+1

代理地址是各種混亂的。看起來像一個垃圾網站。 –

+0

嘗試另一個代理 –

+0

和端口以及 – VladL

回答

5

這似乎工作,但不是與您的代理(不知道unblockwebnow.info的端口號)。 在URI中「:」後添加端口號

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string url = "http://stackoverflow.com"; 
      HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(url); 
      HttpWRequest.Method = "GET"; 

      WebProxy myProxy = new WebProxy(); 

      //United States proxy, from http://www.hidemyass.com/proxy-list/ 
      myProxy.Address = new Uri("http://72.64.146.136:8080"); 
      HttpWRequest.Proxy = myProxy; 

      HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse(); 
      StreamReader sr = new StreamReader(HttpWResponse.GetResponseStream(), true); 
      var rawHTML = sr.ReadToEnd(); 
      sr.Close(); 

      Console.Out.WriteLine(rawHTML); 
      Console.ReadKey(); 
     } 
    } 
} 
+0

如果它不與他的代理一起工作,那麼這不會回答他的問題。 – IronMan84