Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling Redirection in .NET WebRequest

Tags:

c#

webrequest

I'm behind a firewall that asks me to enter credentials before letting me access internet. So my first http request is intercepted and then redirected to a secure server that prompts me to enter my credentials, however the server certificate is not valid and hence, my request.getResponse fails with the exception message: "the underlying connection was closed. Could not establish trust relationship for the SSL/TL secure channel"

WebRequest googleRequest = WebRequest.Create("http://74.125.67.100");
try {WebResponse response = googleRequest.GetResponse();}
catch(WebException ex){System.Console.WriteLine("ex.message");}

Actually what I want is to get the Location header of the response that redirects me, so that I can then establish an ssl connection with the server with invalid certificate. I'll be grateful for suggestions.

like image 246
0fnt Avatar asked Jan 09 '10 13:01

0fnt


1 Answers

Turn off auto-redirection on the initial request. Then you'll be able to pull out the header and do the redirection manually by making a new request.

HttpWebRequest wr = 
(HttpWebRequest)System.Net.WebRequest.Create("http://www.mySite.com"); 
wr.AllowAutoRedirect = false; 
like image 190
Justin Grant Avatar answered Nov 13 '22 13:11

Justin Grant