Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RESTful服务中的摘要认证_Java_Rest_Digest Authentication - Fatal编程技术网

Java RESTful服务中的摘要认证

Java RESTful服务中的摘要认证,java,rest,digest-authentication,Java,Rest,Digest Authentication,我知道java中有类似的东西 urlConnection.setRequestProperty(“授权”、“基本”+ 编码字符串) 用于向rest服务发送用于基本身份验证的授权标头。 摘要身份验证是否有任何等效属性 请建议。以下是一个HttpURLConnection示例: final String username = "username"; final String password = "password"; // Compatible for authentication Basic

我知道java中有类似的东西

urlConnection.setRequestProperty(“授权”、“基本”+ 编码字符串)

用于向rest服务发送用于基本身份验证的授权标头。 摘要身份验证是否有任何等效属性


请建议。

以下是一个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());