Get endpoint IP from URL

2019-03-06 03:36发布

问题:

Using a TCP Client for http post. I get a different result than excepted. No HTTP 200 OK...

Here is my Request:

GET / HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: de-DE
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)
UA-CPU: AMD64
Accept-Encoding: gzip, deflate
Host:www.mywebsite.com
Connection: Keep-Alive
Cache-Control: max-age=0

Here is my Response:

HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Location: http://somewhere.com (url changed)
Content-Length: 0
Date: Fri, 03 Aug 2012 10:38:14 GMT

Its tell me to redirect to "Location". The next request should be a GET to http://somewhere.com with a refer to www.mywebsite.com ?

A other scenario is a https link. Its often a total different IP than the HostEntry returns. How to get the IP ? Is there an easy way in c# ?

回答1:

Yes a 302 means that the url is not at the location you want (the host may have moved it, or prefers you use www instead of the apex). So just use a HTTP GET for the new URL.

In terms of the IP Address, I'm afraid its not so simple, you can have multiple IP Addresses for a hostname

string stackoverflow= "stackoverflow.com"
IPAddress[] addresslist = Dns.GetHostAddresses(stackoverflow);

foreach (IPAddress theaddress in addresslist)
{
   Console.WriteLine(theaddress.ToString());
}

There are lots of situations where more than one IP is used (mainly load balancing situations).



标签: c# http ssl