how to set request hostname in httpclient

你。 提交于 2021-02-11 01:32:44

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!