How to get host name with port from a http or https request

我在jboss容器中部署了两个应用程序(相同的unix框)。如果我从app1收到请求,我需要为app2构建相应的请求。

例如:

如果app1请求是:http//example.com/context? param1 = 123

然后我需要提取" http://example.com/ ",以便我可以构建第二个应用程序的请求。

我试过用:

  HttpServletRequest.getServerName() & 
  HttpServletRequest.getServerPort() & \
  HttpServletRequest.getHeader("host") 

方法,但请求可能是http或https。

如果还有其他更好的方法,请告诉我。谢谢!

于2013年10月25日20:15 问道
库马尔
130128

您可以使用它HttpServletRequest.getScheme()来检索"http"或"https"。

使用它HttpServletRequest.getServerName()应该足以重建您需要的URL部分。

如果您使用的是标准端口(80为http,443为https),则无需在URL中明确放置端口。

编辑:如果您的servlet容器位于终止SSL的反向代理或负载平衡器后面,则它有点棘手,因为请求将以纯http的形式转发到servlet容器。你有几个选择:

1)HttpServletRequest.getHeader("x-forwarded-proto")改为使用; 这只适用于您的负载均衡器正确设置标头(Apache应该是afaik)。

2)在JBoss / Tomcat中配置RemoteIpValve,使其getScheme()按预期工作。同样,这仅在负载均衡器设置正确的标头时才有效。

3)如果上述方法不起作用,您可以在Tomcat / JBoss中配置两个不同的连接器,一个用于http,另一个用于https,如本文所述

于2013年10月25日20:45 回答
大卫莱维斯克
17.7k65068

您可以使用HttpServletRequest.getRequestURLHttpServletRequest.getRequestURI

StringBuffer url = request.getRequestURL();
String uri = request.getRequestURI();
String host = url.substring(0, url.indexOf(uri)); //result
于2013年10月25日20:25 回答
jtahlborn
46.9k5897

如果您想要原始URL,请使用jthalborn描述的方法。如果你想重建网址就像David Levesque解释的那样,这里有一个代码片段:

final javax.servlet.http.HttpServletRequest req = (javax.servlet.http.HttpServletRequest) ...;
final int serverPort = req.getServerPort();
if ((serverPort == 80) || (serverPort == 443)) {
  // No need to add the server port for standard HTTP and HTTPS ports, the scheme will help determine it.
  url = String.format("%s://%s/...", req.getScheme(), req.getServerName(), ...);
} else {
  url = String.format("%s://%s:%s...", req.getScheme(), req.getServerName(), serverPort, ...);
}

您仍需要考虑反向代理的情况:

可以为端口使用常量,但不确定是否有可靠的源,默认端口:

大多数开发人员无论如何都会知道端口80和443,所以常量不是那么有用。

另见这篇类似的帖子

于2015年4月28日9:50 回答
Christophe Roussy
8,91815355

如果您使用负载均衡器和Nginx,请在没有修改代码的情况下对其进行配置。

Nginx的:

proxy_set_header       Host $host;  
proxy_set_header  X-Real-IP  $remote_addr;  
proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;  
proxy_set_header X-Forwarded-Proto  $scheme;  

Tomcat的server.xml引擎:

<Valve className="org.apache.catalina.valves.RemoteIpValve"  
remoteIpHeader="X-Forwarded-For"  
protocolHeader="X-Forwarded-Proto"  
protocolHeaderHttpsValue="https"/> 

如果只修改Nginx配置文件,那么java代码应该是:

String XForwardedProto = request.getHeader("X-Forwarded-Proto");
于2015年8月19日8:50 回答
W.Hao
10114

Refer to http://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html . This is the most concise coverage on url parsing.

answered Feb 16 '16 at 0:53

If your server is running behind a proxy server, make sure your proxy header is set:

proxy_set_header X-Forwarded-Proto  $scheme;

Then to get the right scheme & url you can use springframework's classes:

public String getUrl(HttpServletRequest request) {
    HttpRequest httpRequest = new ServletServerHttpRequest(request);
    UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

    String scheme = uriComponents.getScheme();             // http / https
    String serverName = request.getServerName();     // hostname.com
    int serverPort = request.getServerPort();        // 80
    String contextPath = request.getContextPath();   // /app

    // Reconstruct original requesting URL
    StringBuilder url = new StringBuilder();
    url.append(scheme).append("://");
    url.append(serverName);

    if (serverPort != 80 && serverPort != 443) {
        url.append(":").append(serverPort);
    }
    url.append(contextPath);
    return url.toString();
}
answered Jun 27 at 7:35
Mamun Sardar
1,41642340

Your Answer

Sign up or log in

Sign up using Google
Sign up using Facebook
Sign up using Email and Password

Post as a guest

Name
Email

必需,但从未显示过

点击"发布您的答案",即表示您已经阅读了我们更新的服务条款隐私政策cookie政策,并且您继续使用本网站受这些政策的约束。

不是你要找的答案?浏览标记为 其他问题提出您自己的问题


comments powered by Disqus