问题
I use apache httpclient for send some request to the site, that site check servlet request hostname. It checks hostname like this:
String hostName = request.getRemoteHost();
and when I send a request, hostname always my ip address, not my hostname. my send request code is like this:
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
List<Header> headerList = new ArrayList<>();
headerList.add(new BasicHeader("Host","myCustomHostName"));
HttpClientBuilder client = httpClientBuilder.build();
HttpGet request = new HttpGet("http://siteaddress:8080/getHostname");
request.setHeader(new BasicHeader("Host","myCustomHostnameAgain.just.for.try"));
client.execute(request);
but it not works.
how to set request hostname in httpclient? thx.
回答1:
request.getRemoteHost() does not use headers from the HTTP request. Instead it is doing a reverse DNS lookup. Basically it takes the IP that it got from the request and tries to look that up from DNS information. It is possible that that information doesn't exist in which case the call returns the IP address as a String.
来源:https://stackoverflow.com/questions/36830899/how-to-set-request-hostname-in-httpclient