如何在httpclient中设置请求主机名 [英] how to set request hostname in httpclient

查看:76
本文介绍了如何在httpclient中设置请求主机名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用apache httpclient向站点发送一些请求,该站点检查servlet请求的主机名.它像这样检查主机名:

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();

当我发送请求时,主机名始终是我的IP地址,而不是我的主机名.我的发送请求代码如下:

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);

但不起作用.

如何在httpclient中设置请求主机名?谢谢.

how to set request hostname in httpclient? thx.

推荐答案

request.getRemoteHost()不使用HTTP请求中的标头.相反,它正在执行反向DNS查找.基本上,它使用从请求中获得的IP并尝试从DNS信息中查找该IP.信息可能不存在,在这种情况下,调用将以字符串形式返回IP地址.

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.

这篇关于如何在httpclient中设置请求主机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆