Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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 请求HTTPS URL_Java_Url - Fatal编程技术网

Java 请求HTTPS URL

Java 请求HTTPS URL,java,url,Java,Url,我正在尝试使用URL类从Delicious获取一些数据 URL url = URL(u); url.openStream(); OpenStream在401中失败 我使用的url是 字符串(“https://“+creds+”@api.del.icio.us/v1/posts/recent”) creds是一个base64编码字符串,例如user:pass-encoded,我也尝试了非编码形式,但也失败了,有人能告诉我我缺少了什么吗?宁可使用Apaches-Commons,也要使用Apach

我正在尝试使用URL类从Delicious获取一些数据

URL url = URL(u); 
url.openStream();
OpenStream在401中失败

我使用的url是

字符串(“https://“+creds+”@api.del.icio.us/v1/posts/recent”)


creds是一个base64编码字符串,例如user:pass-encoded,我也尝试了非编码形式,但也失败了,有人能告诉我我缺少了什么吗?

宁可使用Apaches-Commons,也要使用Apaches-Commons

更新: 正如下面指出的,URL类确实有一些处理用户身份验证的机制。但是,OP是正确的,del.icio.us服务返回401,代码如上所示(使用我自己的帐户验证,我提供了正确的凭据..在url中未编码发送它们)

但是,通过手动指定授权标头,稍微修改代码就可以了:

    URL url = new URL("https://api.del.icio.us/v1/posts/suggest");
    byte[] b64 = Base64.encodeBase64("username:pass".getBytes());
    URLConnection conn = url.openConnection();
    conn.setRequestProperty("Authorization", "Basic " + new String(b64));
    BufferedReader r = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    while(r.ready()){
        System.out.println(r.readLine());
    }
更新: 正如下面指出的,URL类确实有一些处理用户身份验证的机制。但是,OP是正确的,del.icio.us服务返回401,代码如上所示(使用我自己的帐户验证,我提供了正确的凭据..在url中未编码发送它们)

但是,通过手动指定授权标头,稍微修改代码就可以了:

    URL url = new URL("https://api.del.icio.us/v1/posts/suggest");
    byte[] b64 = Base64.encodeBase64("username:pass".getBytes());
    URLConnection conn = url.openConnection();
    conn.setRequestProperty("Authorization", "Basic " + new String(b64));
    BufferedReader r = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    while(r.ready()){
        System.out.println(r.readLine());
    }

您需要提供这样的密码验证器

           Authenticator.setDefault( new Authenticator()
            {
              @Override protected PasswordAuthentication getPasswordAuthentication()
              {
                    return new PasswordAuthentication(username, 
                            password.toCharArray()
                            );
              }
            } );
        String credential = username + ":" + password;
        String basicAuth = "Basic " + 
                Base64.encode(credential.getBytes("UTF-8"));
        urlConnection.setRequestProperty("Authorization", basicAuth);
然而,这将增加一个往返,因为它必须等待401。您可以像这样预先设置身份验证标头

           Authenticator.setDefault( new Authenticator()
            {
              @Override protected PasswordAuthentication getPasswordAuthentication()
              {
                    return new PasswordAuthentication(username, 
                            password.toCharArray()
                            );
              }
            } );
        String credential = username + ":" + password;
        String basicAuth = "Basic " + 
                Base64.encode(credential.getBytes("UTF-8"));
        urlConnection.setRequestProperty("Authorization", basicAuth);

您需要提供这样的密码验证器

           Authenticator.setDefault( new Authenticator()
            {
              @Override protected PasswordAuthentication getPasswordAuthentication()
              {
                    return new PasswordAuthentication(username, 
                            password.toCharArray()
                            );
              }
            } );
        String credential = username + ":" + password;
        String basicAuth = "Basic " + 
                Base64.encode(credential.getBytes("UTF-8"));
        urlConnection.setRequestProperty("Authorization", basicAuth);
然而,这将增加一个往返,因为它必须等待401。您可以像这样预先设置身份验证标头

           Authenticator.setDefault( new Authenticator()
            {
              @Override protected PasswordAuthentication getPasswordAuthentication()
              {
                    return new PasswordAuthentication(username, 
                            password.toCharArray()
                            );
              }
            } );
        String credential = username + ":" + password;
        String basicAuth = "Basic " + 
                Base64.encode(credential.getBytes("UTF-8"));
        urlConnection.setRequestProperty("Authorization", basicAuth);

是的,问题在于delicious,如果你有一个新帐户,你需要使用他们api的v2,这就是调用失败的原因。是的,问题在于delicious,如果你有一个新帐户,你需要使用他们api的v2,这就是调用失败的原因。