How to handle HTTP authentication using HttpURLConnection?

前端 未结 2 488
忘掉有多难
忘掉有多难 2020-11-28 09:49

I\'m writing a Java client that POSTs to a HTTP server that requires authentication.
I have to support at least the following three authentication metho

2条回答
  •  無奈伤痛
    2020-11-28 10:27

    Related to @Mat's comment :

    Here is an example used by my team and I :

    import org.apache.commons.codec.binary.Base64;
    
    HttpGet getRequest = new HttpGet(endpoint);
    getRequest.addHeader("Authorization", "Basic " + getBasicAuthenticationEncoding());
    
    private String getBasicAuthenticationEncoding() {
    
            String userPassword = username + ":" + password;
            return new String(Base64.encodeBase64(userPassword.getBytes()));
        }
    

    Hope it helps!

提交回复
热议问题