HttpWebRequest and proxy issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Imran Aziz

    HttpWebRequest and proxy issues

    Hello All,
    I am using HttpWebRequest to fetch webpages in my ASP.net C#
    application. The request works fine without the proxy, but on using the code
    from within a network that uses proxy the request does not work. I tried to
    use the MS code to get around it, but having problems using it. The first
    thing is that the this conversion

    myProxy=(WebPro xy)myWebRequest .Proxy;

    does not work, and I get an error of cannot convert Webproxywrapper to
    WebProxy.
    The MS code is as under.


    // Create a new request to the mentioned URL.
    HttpWebRequest
    myWebRequest=(H ttpWebRequest)W ebRequest.Creat e("http://www.microsoft.c om");
    WebProxy myProxy=new WebProxy();
    // Obtain the 'Proxy' of the Default browser.
    myProxy=(WebPro xy)myWebRequest .Proxy;
    // Print the Proxy Url to the console.
    Console.WriteLi ne("\nThe actual default Proxy settings are
    {0}",myProxy.Ad dress);
    try
    {
    Console.WriteLi ne("\nPlease enter the new Proxy Address that is to be
    set:");
    Console.WriteLi ne("(Example:ht tp://myproxy.com:por t)");
    string proxyAddress;
    proxyAddress =Console.ReadLi ne();
    if(proxyAddress .Length>0)

    {
    Console.WriteLi ne("\nPlease enter the Credentials ");
    Console.WriteLi ne("Username:") ;
    string username;
    username =Console.ReadLi ne();
    Console.WriteLi ne("\nPassword: ");
    string password;
    password =Console.ReadLi ne();
    // Create a new Uri object.
    Uri newUri=new Uri(proxyAddres s);
    // Associate the newUri object to 'myProxy' object so that new
    myProxy settings can be set.
    myProxy.Address =newUri;
    // Create a NetworkCredenti al object and associate it with the Proxy
    property of request object.
    myProxy.Credent ials=new NetworkCredenti al(username,pas sword);
    myWebRequest.Pr oxy=myProxy;
    }
    Console.WriteLi ne("\nThe Address of the new Proxy settings are
    {0}",myProxy.Ad dress);
    HttpWebResponse
    myWebResponse=( HttpWebResponse )myWebRequest.G etResponse();


    Can anyone please help me with this?
    thanks a lot.
    Imran.




  • Joerg Jooss

    #2
    Re: HttpWebRequest and proxy issues

    Imran Aziz wrote:
    [color=blue]
    > Hello All,
    > I am using HttpWebRequest to fetch webpages in my ASP.net C#
    > application. The request works fine without the proxy, but on using
    > the code from within a network that uses proxy the request does not
    > work. I tried to use the MS code to get around it, but having
    > problems using it. The first thing is that the this conversion
    >
    > myProxy=(WebPro xy)myWebRequest .Proxy;
    >
    > does not work, and I get an error of cannot convert Webproxywrapper
    > to WebProxy. The MS code is as under.
    > http://msdn.microsoft.com/library/de...ry/en-us/cpref
    > /html/frlrfSystemNetH ttpWebRequestCl assProxyTopic.a sp[/color]

    That cast is wrong. HttpWebRequest. Proxy is of type IWebProxy. There's
    no guarantee that the class implementing this Interface is really a
    WebProxy object.

    There's really no point in reading the default proxy from the
    HttpWebRequest if you want to use another one. Simply create a new
    WebProxy object with the desired proxy URL, set the required
    credentials and assign this object to the HttpWebRequest' s Proxy
    property.

    Cheers,

    --

    mailto:news-reply@joergjoos s.de

    Comment

    Working...