Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have two applications deployed in a JBoss container (same unix box). If I receive a request from app1, I need to send a corresponding request for app2.

An Example: If app1 requests: http://example.com/context?param1=123, then I need to extract http://example.com/, so that I can send the request for the second app.

I tried using

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

but how can I destinguish between http or https?

like image 565
kumar Avatar asked Oct 25 '13 20:10

kumar


People also ask

What is host name in HTTP request?

A host name is used when a web client makes an HTTP request to a host. The user making the request can specify the IP address of the server rather than the host name, but that is now unusual on the Internet. Host names are more convenient for users than numeric IP addresses.

Does HTTP host include port?

The Host request header specifies the host and port number of the server to which the request is being sent. If no port is included, the default port for the service requested is implied (e.g., 443 for an HTTPS URL, and 80 for an HTTP URL). A Host header field must be sent in all HTTP/1.1 request messages.

How do I find my host and spring boot port?

You can get this information via Environment for the port and the host you can obtain by using InternetAddress . Getting the port this way will only work, if a) the port is actually configured explicitly, and b) it is not set to 0 (meaning the servlet container will choose a random port on startup).

What is HTTP host?

The HTTP host header is a request header that specifies the domain that a client (browser) wants to access. This header is necessary because it is pretty standard for servers to host websites and applications at the same IP address. However, they don't automatically know where to direct the request.


1 Answers

You can use HttpServletRequest.getScheme() to retrieve either "http" or "https".

Using it along with HttpServletRequest.getServerName() should be enough to rebuild the portion of the URL you need.

You don't need to explicitly put the port in the URL if you're using the standard ones (80 for http and 443 for https).

Edit: If your servlet container is behind a reverse proxy or load balancer that terminates the SSL, it's a bit trickier because the requests are forwarded to the servlet container as plain http. You have a few options:

  1. Use HttpServletRequest.getHeader("x-forwarded-proto") instead; this only works if your load balancer sets the header correctly (Apache should afaik).

  2. Configure a RemoteIpValve in JBoss/Tomcat that will make getScheme() work as expected. Again, this will only work if the load balancer sets the correct headers.

  3. If the above don't work, you could configure two different connectors in Tomcat/JBoss, one for http and one for https, as described in this article.

like image 190
David Levesque Avatar answered Sep 28 '22 04:09

David Levesque