2015-01-02 47 views
0

我知道現在沒有在java摘要式身份驗證的RESTful服務

urlConnection.setRequestProperty( 「授權」, 「基本」 + encodedString)類似下面;

用於發送授權報頭的基本認證休息服務。 摘要式身份驗證是否有任何等效屬性?

請建議。

回答

0

下面是HttpURLConnection的一個例子:

final String username = "username"; 
final String password = "password"; 

// Compatible for authentication Basic Digest Ntlm (not working with Negotiate) 
Authenticator.setDefault(new Authenticator() { 
    @Override 
    protected PasswordAuthentication getPasswordAuthentication() { 
     PasswordAuthentication pa = new PasswordAuthentication (username, password.toCharArray()); 
     return pa; 
    } 
}); 

BufferedReader in = null; 
StringBuffer sb = new StringBuffer(); 

try { 
    HttpURLConnection connection = (HttpURLConnection) new URL("http://127.0.0.1:8180/").openConnection(); 

    in = new BufferedReader(new InputStreamReader(connection 
      .getInputStream())); 

    String line; 
    while ((line = in.readLine()) != null) { 
     sb.append(line).append("\n"); 
    } 
} catch (java.net.ProtocolException e) { 
    sb.append("User Or Password is wrong!"); 
    e.printStackTrace(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} finally { 
    try { 
     if (in != null) { 
      in.close(); 
     } 
    } catch (Exception e) { 
     System.out.println("Exception"); 
    } 
} 

System.out.println("The Data is: " + sb.toString());