Am I Running Under HTTP or HTTPS?

Discussion in 'Java' started by MattC, Nov 5, 2005.

  1. MattC

    MattC Guest

    I have a servlet application. Based on a user request the application
    will generate an email that contains a URL to one of the applications
    screens. This application must run in a variety of environments.

    My question is how do I dynamically determine if the URL should use
    HTTP or HTTPS? The only solution I can think of is to do something like
    this:

    String protocol = httpServletRequest.getProtocol();

    if (protocol.indexOf("HTTPS") > -1) {
    // protocol is HTTPS
    } else {
    // protocol is HTTP
    }

    Will this work? Is this the best way to accomplish this?

    Thanks,
     
    MattC, Nov 5, 2005
    #1
    1. Advertising

  2. MattC

    Real Gagnon Guest

    > My question is how do I dynamically determine if the URL should use
    > HTTP or HTTPS? The only solution I can think of is to do something like
    > this:
    >
    > String protocol = httpServletRequest.getProtocol();
    >
    > if (protocol.indexOf("HTTPS") > -1) {
    > // protocol is HTTPS
    > } else {
    > // protocol is HTTP
    > }


    Use the servlet request's isSecure() or getAuthType() methods.

    Or you can check these HTTP headers : CERT_KEYSIZE , CERT_KEYSIZE,
    HTTPS_KEYSIZE

    This will check if the current connection is secured or not.

    To check if the client can handle a secured connection, check this HowTo
    at http://www.rgagnon.com/jsdetails/js-0088.html

    Bye.
    --
    Real Gagnon from Quebec, Canada
    * Looking for Java or PB code examples ? Visit Real's How-to
    * http://www.rgagnon.com/howto.html
     
    Real Gagnon, Nov 5, 2005
    #2
    1. Advertising

  3. MattC

    Roedy Green Guest

    On 5 Nov 2005 07:37:35 -0800, "MattC" <>
    wrote, quoted or indirectly quoted someone who said :

    >String protocol = httpServletRequest.getProtocol();
    >
    >if (protocol.indexOf("HTTPS") > -1) {
    > // protocol is HTTPS
    >} else {
    > // protocol is HTTP
    >}


    I think is this more likely to work:

    if ( protocol.equalsIgnoreCase( "https" ) )

    Possibly the protocol is always presented as lower case, in which case
    you could say

    if ( protocol.equals( "https" ) )
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 5, 2005
    #3
  4. MattC

    MattC Guest

    Ah yes, isSecure() that's the ticket.

    I had a sence that there was a cleaner solution I just wasn't sure what
    is was. - Thanks.
     
    MattC, Nov 7, 2005
    #4
  5. MattC

    intertester

    Joined:
    Sep 14, 2012
    Messages:
    1
    I tried isSecure().
    But it returns true even if it is http. How did you resolve this?
     
    intertester, Sep 14, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hshdude
    Replies:
    12
    Views:
    1,107
    Dimitri Maziuk
    Nov 4, 2004
  2. bronby
    Replies:
    1
    Views:
    667
    Andrew Thompson
    Jul 15, 2005
  3. Axel
    Replies:
    8
    Views:
    1,260
    Adrienne Boswell
    Apr 27, 2009
  4. jotto
    Replies:
    4
    Views:
    441
    jotto
    Oct 2, 2006
  5. Naveen Dhanuka
    Replies:
    1
    Views:
    318
Loading...

Share This Page