13
function demo(request,response){
        request.getScheme() is returning http instead of returning https.
        System.out.println(""+request.getScheme());
}

output:http

--above function demo is being called from main method but it prints http instead it should print https while working on internet server.

Sumit
  • 141
  • 1
  • 1
  • 4
  • can you verify what is full uri by using request.getRequestURI()in function – Dev Sep 18 '14 at 11:33
  • 3
    This does not look like Java. – Henry Sep 18 '14 at 11:38
  • on the basis of scheme i am making a url to hit a new link from my application but because i am getting http from request.getScheme().i am unable to make correct url path. @Henry this is demo function.. use to ask question.. sry for not commenting //is returning http instead of returning https – Sumit Sep 18 '14 at 11:41

3 Answers3

9

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 you can use springframework's classes:

HttpRequest httpRequest = new ServletServerHttpRequest(request); //request is HttpServletRequest
UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(httpRequest).build();

String scheme = uriComponents.getScheme(); // http/https
Mamun Sardar
  • 2,679
  • 4
  • 36
  • 44
6

See the answer https://stackoverflow.com/a/19599143/1524502, and note the issues about being behind a reverse proxy or load balancer. Most likely, that is your problem.

The answerer in that question recommended using

request.getHeader("x-forwarded-proto")

instead, though that is dependent on your load balancer setting the header correctly.

Community
  • 1
  • 1
jonnybot
  • 2,435
  • 1
  • 32
  • 56
3

I used to have a similar problem with getScheme()

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

I've solved using "//" instead:

String basePath = "//"+request.getServerName()+":"+request.getServerPort()+path+"/";