开发者

Setting proxy and get response

开发者 https://www.devze.com 2022-12-24 19:09 出处:网络
I want to implement p开发者_如何学Gorogram by c# application. first of all I need web request to a remote server and set proxy and view sourcecode of that page.

I want to implement p开发者_如何学Gorogram by c# application. first of all I need web request to a remote server and set proxy and view sourcecode of that page.

How I can write codes is there any code?


You may want to have a look at this.


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();


If you are developing ASP.NET websites you can also set the proxy globally for all web requests in the web.config like so:

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消