如何使用 HttpURLConnection 处理 HTTP 身份验证?

发布于 2024-10-15 07:43:07 字数 686 浏览 3 评论 0原文

我正在编写一个 Java 客户端,它会 POST 到需要身份验证的 HTTP 服务器。
我必须至少支持以下三种身份验证方法:基本、摘要或协商。另外,POST 可能非常大(超过 2MB),所以我需要使用流式传输。 正如 HttpURLConnection< 的记录/a>

启用输出流时,无法自动处理身份验证和重定向。如果需要身份验证或重定向,则在读取响应时将抛出 HttpRetryException。

所以,我需要自己处理身份验证。我搜索并再次搜索,寻找一种使用已经编码的类的方法 - 但找不到任何方法......

我只能从 此处(因为它们是带有类路径例外的 GPLv2)。这是正确的方法吗?

谢谢。

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 methods: Basic, Digest or Negotiate. Additionally the POST may be very large (over 2MB), so I need to use streaming.
As is documented for HttpURLConnection

When output streaming is enabled, authentication and redirection cannot be handled automatically. A HttpRetryException will be thrown when reading the response if authentication or redirection are required.

So, I need to handle authentication myself. I searched, and searched again, for a way to employ the, already coded, classes - but found no way...

I could just pluck the needed sources from here (as they are GPLv2 with Classpath exception). Is this the right way?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梨涡 2024-10-22 07:43:07

您需要输出流吗? HttpURLConnection 绝对支持使用 Authenticator 类进行身份验证,请参阅:Http 身份验证

更新:如果Authenticator不是一个选项,您可以通过向 HTTP 请求添加额外的标头来手动执行 HTTP 基本身份验证。尝试以下代码(未经测试):

String userPassword = username + ":" + password;
String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());
URLConnection uc = url.openConnection();
uc.setRequestProperty("Authorization", "Basic " + encoding);
uc.connect();

Do you need output streaming? The HttpURLConnection most definitely supports authentication with the Authenticator class, see: Http Authentication.

Update: In case the Authenticator is not an option, you can manually do HTTP basic authentication by adding an extra header to your HTTP request. Try the following code (untested):

String userPassword = username + ":" + password;
String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());
URLConnection uc = url.openConnection();
uc.setRequestProperty("Authorization", "Basic " + encoding);
uc.connect();
醉城メ夜风 2024-10-22 07:43:07

与 @Mat 的评论相关:

这是我和我的团队使用的一个示例:

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()));
    }

希望有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文