Home > Archive > ASP .NET > August 2005 > HttpWebRequest and proxy issues
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
HttpWebRequest and proxy issues
|
|
| Imran Aziz 2005-08-29, 7:02 pm |
| 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=(WebProxy)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/d...o
pic.asp
// Create a new request to the mentioned URL.
HttpWebRequest
myWebRequest=(HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
WebProxy myProxy=new WebProxy();
// Obtain the 'Proxy' of the Default browser.
myProxy=(WebProxy)myWebRequest.Proxy;
// Print the Proxy Url to the console.
Console.WriteLine("\nThe actual default Proxy settings are
{0}",myProxy.Address);
try
{
Console.WriteLine("\nPlease enter the new Proxy Address that is to be
set:");
Console.WriteLine("(Example:http://myproxy.com:port)");
string proxyAddress;
proxyAddress =Console.ReadLine();
if(proxyAddress.Length>0)
{
Console.WriteLine("\nPlease enter the Credentials ");
Console.WriteLine("Username:");
string username;
username =Console.ReadLine();
Console.WriteLine("\nPassword:");
string password;
password =Console.ReadLine();
// Create a new Uri object.
Uri newUri=new Uri(proxyAddress);
// Associate the newUri object to 'myProxy' object so that new
myProxy settings can be set.
myProxy.Address=newUri;
// Create a NetworkCredential object and associate it with the Proxy
property of request object.
myProxy.Credentials=new NetworkCredential(username,password);
myWebRequest.Proxy=myProxy;
}
Console.WriteLine("\nThe Address of the new Proxy settings are
{0}",myProxy.Address);
HttpWebResponse
myWebResponse=(HttpWebResponse)myWebRequ
est.GetResponse();
Can anyone please help me with this?
thanks a lot.
Imran.
| |
| Joerg Jooss 2005-08-31, 3:58 am |
| Imran Aziz wrote:
> 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=(WebProxy)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/d...ary/en-us/cpref
> /html/ frlrfSystemNetHttpWebRequestClassProxyTo
pic.asp
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,
--
http://www.joergjooss.de
mailto:news-reply@joergjooss.de
|
|
|
|
|