在 URL 对象中设置自定义 HTTP 请求标头不起作用

发布于 2024-11-16 22:25:02 字数 615 浏览 10 评论 0原文

我正在尝试使用 HTTP 从 IP 摄像机获取图像。相机需要 HTTP 基本身份验证,因此我必须添加相应的请求标头:

URL url = new URL("http://myipcam/snapshot.jpg");
URLConnection uc = url.openConnection();
uc.setRequestProperty("Authorization", 
  "Basic " + new String(Base64.encode("user:pass".getBytes())));

// outputs "null"
System.out.println(uc.getRequestProperty("Authorization"));

稍后我将 url 对象传递给 ImageIO.read(),并且,您可以我猜,尽管 userpass 是正确的,但我收到了 HTTP 401 Unauthorized。

我做错了什么?

我也尝试过 new URL("http://user:pass@myipcam/snapshot.jpg"),但这也不起作用。

I am trying to fetch an image from an IP camera using HTTP. The camera requires HTTP basic authentication, so I have to add the corresponding request header:

URL url = new URL("http://myipcam/snapshot.jpg");
URLConnection uc = url.openConnection();
uc.setRequestProperty("Authorization", 
  "Basic " + new String(Base64.encode("user:pass".getBytes())));

// outputs "null"
System.out.println(uc.getRequestProperty("Authorization"));

I am later passing the url object to ImageIO.read(), and, as you can guess, I am getting an HTTP 401 Unauthorized, although user and pass are correct.

What am I doing wrong?

I've also tried new URL("http://user:pass@myipcam/snapshot.jpg"), but that doesn't work either.

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

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

发布评论

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

评论(3

农村范ル 2024-11-23 22:25:02

在类sun.net.www.protocol.http.HttpURLConnection,它扩展了java.net.HttpURLConnection,请求安全敏感信息时,以下方法 getRequestProperty(String key) 被重写以返回 null

public String getRequestProperty(String key) {
    // don't return headers containing security sensitive information
    if (key != null) {
        for (int i = 0; i < EXCLUDE_HEADERS.length; i++) {
        if (key.equalsIgnoreCase(EXCLUDE_HEADERS[i])) {
            return null;
        }
        }
    }
    return requests.findValue(key);
}

以下是 EXCLUDE_HEADERS 的声明:

// the following http request headers should NOT have their values
// returned for security reasons.
private static final String[] EXCLUDE_HEADERS = {
    "Proxy-Authorization", "Authorization" };

这就是 uc.getRequestProperty("Authorization") 上的 null 的原因。您是否尝试过使用 Apache 的 HttpClient

In class sun.net.www.protocol.http.HttpURLConnection, which extends java.net.HttpURLConnection, the following method getRequestProperty(String key) was overridden to return null when requesting security sensitive information.

public String getRequestProperty(String key) {
    // don't return headers containing security sensitive information
    if (key != null) {
        for (int i = 0; i < EXCLUDE_HEADERS.length; i++) {
        if (key.equalsIgnoreCase(EXCLUDE_HEADERS[i])) {
            return null;
        }
        }
    }
    return requests.findValue(key);
}

Here is the declaration for EXCLUDE_HEADERS:

// the following http request headers should NOT have their values
// returned for security reasons.
private static final String[] EXCLUDE_HEADERS = {
    "Proxy-Authorization", "Authorization" };

That's why you're having a null on uc.getRequestProperty("Authorization"). Have you tried using HttpClient from Apache?

遥远的她 2024-11-23 22:25:02

问题已解决。它不起作用,因为我将 url 传递给 ImageIO.read()

相反,传递 uc.getInputStream() 使其正常工作。

Issue resolved. It didn't work because I was passing url to ImageIO.read().

Instead, passing uc.getInputStream() got it working.

仅冇旳回忆 2024-11-23 22:25:02

您是否尝试过对 URLConnectionHttpURLConnection 进行子类化并重写 getRequestProperty() 方法?

Have you tried to subclass URLConnection or HttpURLConnection and override the getRequestProperty() method?

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