HTTP-GET through a proxy

Hey I am working on a proxy testing program as a personal project for a friend of mine, and for the life of me I cannot figure it out.  I have searched the forums and MSDN till im blue in the face for something I figured would be very simple.  maybe you can help,  all I need to do is do a HTTP-GET to a specified URL ( his website ) through a third party proxy ( my proxy ) from c# application.  Can anyone point me in the right direction


Answer this question

HTTP-GET through a proxy

  • LuisCabrera

    You can say
    WebProxy wp = new WebProxy("<my proxy server", true);

    wp.UseDefaultCredentials = true // OR
    wp.Credentials = new NetworkCredential(pass the user domain and password)
    //Set the proxy
    Req.Proxy = wp;



  • Fat Controller

    if this is .NEt framework 1.1 then you can use

    proxy.Credentials = CredentialCache.DefaultCredentials

  • Russ555

    Try this



    using System;

    using System.Net;

    using System.IO;

    using System.Collections.Generic;

    using System.Text;

    namespace ConsoleApplication1

    {

    class Program

    {

    static void Main(string[] args)

    {

    HttpWebResponse Res = null;

    Stream s = null;

    try

    {

    HttpWebRequest Req = WebRequest.Create("http://www.microsoft.com") as HttpWebRequest;

    //WebProxy wp = new WebProxy("<my proxy server", true);

    //Req.Proxy = wp;

    Res = Req.GetResponse() as HttpWebResponse;

    s = Res.GetResponseStream();

    StreamReader sr = new StreamReader(s);

    Console.WriteLine(sr.ReadToEnd());

    sr.Close();

    }

    catch (Exception ex)

    {

    Console.WriteLine(ex);

    }

    finally

    {

    if (Res != null) Res.Close();

    if( s != null) s.Close();

    }

    }

    }

    }





     


  • ChipDowns

    Also, if you don't want to set your proxy per request, you can set it for all requests via an application configuration file in V2.0:

    <configuration> Element
      system.Net Element (Network Settings)
        defaultProxy Element (Network Settings)
          proxy Element (Network Settings)

    <proxy   autoDetect="true|false|unspecified"    bypassonlocal="true|false|unspecified"   proxyaddress="uriString"  scriptLocation="uriString"   usesystemdefault="true|false|unspecified "  />

    autoDetect

    Specifies whether the proxy is automatically detected. The default value is unspecified.

    bypassonlocal

    Specifies whether the proxy is bypassed for local resources. Local resources include the local server (http://localhost, http://loopback, or http://127.0.0.1) and a URI without a period (http://webserver). The default value is unspecified.

    proxyaddress

    Specifies the proxy URI to use.

    scriptLocation

    Specifies the location of the configuration script.

    usesystemdefault

    Set to false to get V2.0 proxy behavior.



  • Suj

    This code is exactly what I am looking for with one minor exception.  We require authentication through our Microsoft ISA server.  I see that the WebProxy can pass the ICredential paramater.  However, as I read the msdn on ICredential, I do not understand how to implement it. 

    How would this code need to be modified to pass the credentials to the proxy server

    Any help would be appreciated.

    Paul

  • cparsons

    Thank you very much for your help.  I was able to get the application to work using the "new Network Credential" line.

    However, I was surprized that the  .UseDefaultCredentials method was not available to me.  Perhaps I was initializing something incorrectly.

    Thanks, again. 

  • HTTP-GET through a proxy