You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IP摄像头视频流拉取报HTTP 401错误求助

Fixing 401 Unauthorized Error When Pulling IP Camera Stream in Java

Hey there, let's work through this 401 error you're hitting when trying to access your IP camera. That error means the server isn't accepting your authentication, so let's break down the issues and fixes step by step:

First: Fix the URL Escape Character

Looking at your code, the URL uses & instead of a plain & to separate the user and pwd parameters. & is an HTML escape sequence, but in Java URLs you need to use the actual & character. This is likely one reason the camera isn't parsing your credentials correctly.

Change this line:

String Cam1Jpeg_url = "http://192.168.43.1:8080/snapshot.cgi?user=admin&pwd=123456";

To:

String Cam1Jpeg_url = "http://192.168.43.1:8080/snapshot.cgi?user=admin&pwd=123456";

Second: Use HTTP Basic Authentication (Most IP Cameras Require This)

Even with the corrected URL, many IP cameras don't accept credentials as URL parameters—they expect HTTP Basic Authentication headers instead. Here are two ways to handle this in your code:

Option 1: Use Authenticator for Global Authentication

Add this at the start of your main method to set up default authentication for all requests:

Authenticator.setDefault(new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("admin", "123456".toCharArray());
    }
});

Then you can simplify your URL to just:

String Cam1Jpeg_url = "http://192.168.43.1:8080/snapshot.cgi";

Option 2: Set Authorization Header Directly on the Connection

If you prefer more control over individual requests, use URLConnection to manually add the auth header:

// Replace url.openStream() with this block
URLConnection connection = url.openConnection();
String userCredentials = "admin:123456";
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
connection.setRequestProperty("Authorization", basicAuth);
InputStream is = connection.getInputStream();

(Note: You'll need to import java.util.Base64 for this approach.)

Quick Additional Checks

  • Double-check that the username admin and password 123456 match exactly what's configured on your camera—typos are a super common culprit here!
  • Verify your camera allows HTTP access (some default to HTTPS or restrict access to specific IP addresses).
  • Test the corrected URL in a web browser first—if it prompts you for a login, that confirms Basic Auth is required.

Here's the full modified code using the Authenticator approach (plus a small delay to avoid overwhelming the camera):

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import javax.imageio.ImageIO;
import com.googlecode.javacv.CanvasFrame;

public class TestconnectionCamera {
    public static void main(String[] args) throws IOException {
        // Set up Basic Authentication
        Authenticator.setDefault(new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("admin", "123456".toCharArray());
            }
        });

        CanvasFrame CamWindow = new CanvasFrame("Camera");
        String Cam1Jpeg_url = "http://192.168.43.1:8080/snapshot.cgi";
        URL url = new URL(Cam1Jpeg_url);
        
        while(true){
            InputStream is = url.openStream();
            BufferedImage image = ImageIO.read(is);
            CamWindow.showImage(image);
            is.close();
            // Add a small delay to prevent spamming the camera
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者user9045870

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:53:19