在c#中测试代理服务器

时间:2014-04-10 14:02:36

标签: c# proxy

我正在试图找出代理服务器如何工作&使用Fiddler我试图看看我是否能看到代理服务器。

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

1 个答案:

答案 0 :(得分: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;
    }
}

至于在Fiddler中显示完整的URL,我不确定。也许搞乱工具 - >提琴手选项 - > Gateway可以帮到你。