Obtain the server response URL with c #

-1

Good afternoon, I've been trying for a while and I do not see that I'm approaching the solution.

I need to send a url of the type www.dominio.com for the server to return www.dominio.com/index.html, for example. Come on, the domain default page.

As I have the logic, using a webbrowser object would complicate my life a lot, instead, any other class like HttpWebResponse, WebClient or whatever you want, serves me.

Thank you very much in advance.

    
asked by José Carbonell 13.07.2017 в 16:37
source

1 answer

1

As far as I know, you can only get that information if the server redirects you with a 301/302 and sends you the new url in the "Location" header. For this you must deactivate the automatic redirection and check the headers:

var http = (HttpWebRequest)WebRequest.Create("http://www.q-protex.com");
http.AllowAutoRedirect = false;
var response = http.GetResponse();
Console.WriteLine(response.Headers["Location"]);

But if the server does not use redirection and has an index.html, index.php or some other file set as the default, this data does not appear in the headers.

    
answered by 13.07.2017 в 17:06