3

我必须通过我的 java 代码访问 https 站点。但它返回 401 响应。我在下面包含了我的代码。

try {        
 URL u = new URL(url);
 HttpURLConnection http = (HttpURLConnection)u.openConnection();
 http.setAllowUserInteraction(true);
 http.connect();

 String userpassword = "HP:M0lveau";
 byte[] encoded = Base64.encodeBase64(userpassword.getBytes());
 String encodedAuthorization = new String(encoded);

 http.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
 InputStream is = http.getInputStream();

 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
 StringBuilder stringBuilder = new StringBuilder();
 String line = null;
 while ((line = reader.readLine()) != null) {
   stringBuilder.append(line + "\n");
 }
 return stringBuilder.toString();

} catch (IOException ioe) {
 logger.debug("fetchDataFromServer:IOException");
 return null;
}

请尽早提供帮助..提前谢谢....

4

3 回答 3

2

401响应意味着请求需要用户认证。在这里寻求帮助

有类似的东西

public static String userNamePasswordBase64(String username, String password)
{
    return "Basic " + base64Encode (username + ":" + password);
}

static public String base64Encode(String s)
{
    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    Base64OutputStream out = new Base64OutputStream(bout);
    try
    {
        out.write(s.getBytes());
        out.flush();
    } catch (IOException e)
    {
    }
return bout.toString();
}

然后使用

http.setRequestProperty ("Authorization",userNamePasswordBase64("HP","M01veau"));
http.connect();

还要手动检查这些凭据在给定的 url 上是否正常工作。另请参阅这个很棒的教程

于 2011-01-21T05:23:25.550 回答
2

我明白了..这是我的代码。

private static String fetchDataFromServer() throws HttpException, IOException, NoSuchAlgorithmException, KeyManagementException {

        logger.trace("__ENTERING CluemasterData::fetchDataFromServer()");

        try {
            URL u = new URL("https://....");
            HttpsURLConnection http = (HttpsURLConnection)u.openConnection();
            Authenticator.setDefault( new MyAuthenticator() );
            http.setAllowUserInteraction(true);
            http.setRequestMethod("GET");
            http.connect();

            InputStream is = http.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder stringBuilder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null)
            {
                stringBuilder.append(line + "\n");
            }
            return stringBuilder.toString();    
        }
        catch (HttpException he) {

            return null;
        } 
        catch (IOException ioe) {

            return null;
        }

    }

MyAuthenticator 类

import java.net.Authenticator;
import java.net.PasswordAuthentication;

class MyAuthenticator extends Authenticator
   {
   /**
   * Called when password authorization is needed.
   * @return The PasswordAuthentication collected from the
   * user, or null if none is provided.
   */
   protected PasswordAuthentication getPasswordAuthentication()
      {
      return new PasswordAuthentication ( "username", "password".toCharArray() );
      }
   }
于 2011-01-24T12:58:11.733 回答
2

尝试在调用 connect() 之前设置 Authorization 标头

于 2011-01-21T08:55:14.033 回答