2

I want to check the status code for a web url without using the IP address as I have many websites configured with the same IP and only hostname is different for them. The HttpWebRequest resolves the IP Address and use it.

Is there an alternative?

Paul Turner
  • 38,949
  • 15
  • 102
  • 166
  • What are you trying to achieve? – R. Martinho Fernandes Feb 01 '11 at 14:31
  • 3
    You can't communicate over TCP/IP without having or resolving an IP address. I have strong reason to believe that the HttpWebRequest class behaves correctly, resolves the IP address, and uses the Host header to indicate which "hostname" it's trying to communicate with. So what actual problem are you experiencing that leads you to believe that it's broken? – Damien_The_Unbeliever Feb 01 '11 at 14:41

2 Answers2

2

In the sample code for the HttpWebRequest class listed on MSDN:

HttpWebRequest myReq =
    (HttpWebRequest)WebRequest.Create("http://www.contoso.com/");

Does not require an IP.

Edit:
As a test, I threw this into a throw away application I have:

HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://domain1.mylocalhost.com/");

HttpWebRequest myReq2 = (HttpWebRequest)WebRequest.Create("http://domain2.mylocalhost.com/");

WebResponse myReqResponse = myReq.GetResponse();
WebResponse myReq2Response = myReq2.GetResponse();

long x = myReqResponse.ContentLength;
long y = myReq2Response.ContentLength;

MessageBox.Show((x == y).ToString());

I have domain1 pointing to a valid site that functions. I have domain2 pointing to a site that delivers an error 500 for all requests. Both are on the same IP. The code above generates a content length of 17320 for myReqResponse and myReq2Response throws an exception delivering up the error 500. An IP address is not necessary and the resolution behind the scenes does not affect its behavior.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • @Martinho Fernandes - I upvoted that comment just for the proper usage of the term (sic). – Joel Etherton Feb 01 '11 at 14:38
  • Martinho...Thanks for the prompt response. – Anoop George Thomas Feb 01 '11 at 14:39
  • As I mentioned in the question, I have to check a list of websites' http status which are hosted on the same server with same ip and different host names – Anoop George Thomas Feb 01 '11 at 14:43
  • @Anoop if that's all you want the HttpWebRequest is more than fit for it. – R. Martinho Fernandes Feb 01 '11 at 14:44
  • HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(siteurl); httpReq.AllowAutoRedirect = false; HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse(); int stscd = (int)httpRes.StatusCode; httpRes.Close(); – Anoop George Thomas Feb 01 '11 at 14:48
  • this is the code i m using and in the siteurl i m passing the site url...But all the websites are browsing the same ip – Anoop George Thomas Feb 01 '11 at 14:50
  • @Anoop George Thomas - You should post this code as an edit to your question. It might get more attention that way and will be much more readable. – Joel Etherton Feb 01 '11 at 15:04
  • @Anoop: and what were you expecting? You cannot access an HTTP server without an IP. If the sites are all served on the same IP, they're all accessed through the same IP. If you tried to use some other IP it would fail, because there's no server there. – R. Martinho Fernandes Feb 01 '11 at 15:08
  • @Anoop - This may not be the best method for performing such a check. Server name resolution errors and application exceptions (404, 500, etc) throw a System.Net.WebException and do not produce a response that has a StatusCode. – Joel Etherton Feb 01 '11 at 15:21
0

I have set up a web site with host header names before. As the name implies, it is the header (from the client) that causes it to go to the correct site on the server. If the header is not supplied, the server won't route to the correct site.

Have you tried adding the host header to the headers collection of the request?

Dim req As System.Net.HttpWebRequest = DirectCast(System.Net.WebRequest.Create("http://www.whatever.com/"), System.Net.HttpWebRequest)
req.Headers.Add("Host", "www.whatever.com")

You can use http://web-sniffer.net/ to determine what other headers you may need to pass to get this working.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212