2014-04-10 4 views
0

프록시 서버 작동 방식을 파악하려고합니다. & 피들러를 사용하여 프록시 서버를 볼 수 있는지 확인하려고합니다.C에서 프록시 서버 테스트 #

class Program{ 
static void Main(string[] args) 
     { 
setProxies(); 
} 

private static void setProxies() 
{ 
    string fullproxyaddress = "http://ec2-100-100-111-555.compute-1.amazonaws.com/OsProxy/getpage.aspx?p="; 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("www.google.com"); 
    request.Accept = "text/html"; 
    request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-US"); 
    request.Method = "GET"; 
    request.Headers.Add("Accept-Encoding", "gzip, deflate"); 
    Uri newUri = new Uri(fullproxyaddress); 
    WebProxy myProxy = new WebProxy(); 
    myProxy.Address = newUri; 
    request.Proxy = myProxy; 
    try 
     { 
       string html = new TimedWebClient { Timeout = 360000 }.DownloadString("www.google.com"); 
       html = HttpUtility.HtmlDecode(html); 
    }catch... 
    } 
    } 
    class TimedWebClient : WebClient 
     { 
      // Timeout in milliseconds, default = 600,000 msec 
      public int Timeout { get; set; } 
      public Encoding enc { get; set; } 

      public TimedWebClient() 
      { 
       this.Timeout = 600000; 
       this.Encoding = Encoding.UTF8; 
      } 

      protected override WebRequest GetWebRequest(Uri address) 
      { 
       var objWebRequest = base.GetWebRequest(address); 
       objWebRequest.Timeout = this.Timeout; 
       objWebRequest.Proxy = this.Proxy; 
       return objWebRequest; 
      } 
     } 

피들러에서 나는 이것을 실행할 때 fullproxyaddr + www.google.com을 볼 것으로 예상됩니다. 호스트에서 www.google.com과 URL의 '/'만 보는 이유는 무엇입니까?

감사 코드와 R

+0

프록시를 사용하여 코드를 작동 시키거나 Fiddler에서 프록시와 함께 URL을 표시하는 방법을 알아 내려고 시도하고 있습니까? 귀하의 코드가 프록시를 사용하지 않는 것 같습니다. –

+0

@DanielSimpkins, 나는 프록시를 작동 시키려고 노력하고 있습니다. 그리고 Fiddler에서 이것을 보려고합니다. – newbieCSharp

답변

0

당신이이 방법, 당신이 첨부 프록시 정보를 사용하여 만든 HttpWebRequest 객체를 사용하고 있지 않습니다. TimedWebClient 클래스를 사용하여 요청할 경우에는 필요하지 않지만 프록시 정보를 설정해야합니다.

다음은 Fiddler 프록시를 사용하는 작동 예제입니다. 프록시 정보를 바꾸면 제대로 작동합니다.

class Program 
{ 
    static void Main(string[] args) 
    { 
     setProxies(); 
     Console.ReadLine(); 
    } 

    private static void setProxies() 
    { 
     //Set our proxy information 
     string fullproxyaddress = "http://localhost:8888"; 
     WebProxy myProxy = new WebProxy(fullproxyaddress); 
     myProxy.Credentials = new NetworkCredential("1", "1"); 

     try 
     { 
      //Initialize our object using the created proxy 
      //Make the request 
      string html = new TimedWebClient { Timeout = 360000, Proxy = myProxy }.DownloadString("http://www.google.com"); 
      html = HttpUtility.HtmlDecode(html); 
      Console.Write(html); 
     } 
     catch { Console.Write("Error!"); } 
    } 
} 
class TimedWebClient : WebClient 
{ 
    // Timeout in milliseconds, default = 600,000 msec 
    public int Timeout { get; set; } 
    public Encoding enc { get; set; } 

    public TimedWebClient() 
    { 
     this.Timeout = 600000; 
     this.Encoding = Encoding.UTF8; 
    } 

    protected override WebRequest GetWebRequest(Uri address) 
    { 
     var objWebRequest = base.GetWebRequest(address); 
     objWebRequest.Timeout = this.Timeout; 
     objWebRequest.Proxy = this.Proxy; 
     return objWebRequest; 
    } 
} 

전체 URL을 피들러에 표시하는 한 확실하지 않습니다. 어쩌면 도구 -> 피들러 옵션 -> 게이트웨이를 둘러 보는 것이 도움이 될 수 있습니다.

+0

google.com에서 작동합니다. "http://www.dotnetperls.com/uri"를 시도하면 왜 작동하지 않습니까? 404 예외가 발생합니다. – newbieCSharp

+0

확실하지 않습니다. 방금 테스트 해본 결과 나에게 도움이되었습니다. 거기에 전체 URL을 넣었습니까 (http : // www ....)? –

+0

http://www.dotnetpearls.com을 넣으면 작동하지만 http://www.dotnetpearls.com/uri를 넣으면 404 예외가 발생합니다 – newbieCSharp