设置代理并获得响应

时间:2010-03-29 08:05:53

标签: c# .net

我想通过c#应用程序实现程序。首先,我需要向远程服务器发送Web请求并设置代理并查看该页面的源代码。

我如何编写代码是否有任何代码?

3 个答案:

答案 0 :(得分:2)

您可能需要查看this

答案 1 :(得分:0)

string addr = textBox1.Text;
        string result = "";

        Uri uri = WebRequest.DefaultWebProxy.GetProxy(new Uri(addr));

        WebProxy myProxy = new WebProxy("ems28.your-freedom.de", 443);
        myProxy.Credentials = new NetworkCredential("user", "pass");
        myProxy.BypassProxyOnLocal=true;

        try{
        WebRequest req = WebRequest.Create(addr);
        req.Proxy = myProxy;

        HttpWebResponse response = (HttpWebResponse)req.GetResponse(); System.IO.Stream stream = response.GetResponseStream();
        System.Text.Encoding ec = System.Text.Encoding.GetEncoding("utf-8");
        System.IO.StreamReader reader = new System.IO.StreamReader(stream, ec);
        char [] chars = new Char[256];
        int count = reader.Read(chars, 0, 256);
        while(count > 0)
        {
        string str = new String(chars, 0, 256);
        result = result + str;
        count = reader.Read(chars, 0, 256);
        }
        response.Close();
        stream.Close();
        reader.Close();
        }
        catch(Exception exp)
        {
        string str = exp.Message;
        }
        MessageBox.Show(result);

        webBrowser1.Url = uri;
        webBrowser1.GoForward();

答案 2 :(得分:0)

如果您正在开发ASP.NET网站,您还可以在web.config中为所有Web请求全局设置代理,如下所示:

<configuration>
   <system.net>
      <defaultProxy>
         <proxy
            usesystemdefault = "false"
            proxyaddress="http://proxyserver"
            bypassonlocal="true"
         />
      </defaultProxy>
   </system.net>
</configuration>

请参阅http://support.microsoft.com/default.aspx?scid=kb;en-us;318140