代码之家  ›  专栏  ›  技术社区  ›  kamiar3001

设置代理并获取响应

  •  0
  • kamiar3001  · 技术社区  · 14 年前

    我想用C应用程序来实现程序。首先,我需要向远程服务器发出web请求,并设置代理和查看该页的源代码。

    我怎么写代码有代码吗?

    3 回复  |  直到 14 年前
        1
  •  2
  •   Simon Linder    14 年前

    你可能想看看 this .

        2
  •  0
  •   kamiar3001    14 年前
    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();
    
        3
  •  0
  •   Dan Diplo    14 年前

    如果您正在开发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