how to make http request same url with different ip's?

3k views Asked by At

I would like to send an HTTP request and get the response body, but I have URL that is stored on multiple servers. Let's say I have this list:

www.mysite.com 192.168.1.31
www.mysite.com 192.168.1.32

and I want to make the request to all the the different servers (different IP's) but same URL

Is there any option to do that in Java?

1

There are 1 answers

0
Brian White On

Yes, you can do that. Define the URL as being what you want and using the IP address in place of a domain name. I.E. http://192.168.1.31/path/to/index.html Then add the "Host: www.mysite.com" header before issuing the request. Any HTTP/1.1 compliant server will use the value of that header as the domain with which it was accessed.

Exactly how you accomplish this depends on whatever library you're using to make the connection but they should all have the ability to set arbitrary headers -- just make sure it doesn't overwrite your custom "Host" header with one of its own from the URL. See this other StackOverflow question for examples of how to implement an HTTP request.

This works because on the wire it's all IP. You can try it yourself using nc, socket, or even telnet.

(open TCP connection to 192.168.1.31 port 80)
GET /path/to/index.html HTTP/1.1
Host: www.mysite.com
                                        <--blank line signals end of headers