Setting custom HTTP request headers in an URL object doesn't work.

Posted by Blagovest Buyukliev on Stack Overflow See other posts from Stack Overflow or by Blagovest Buyukliev
Published on 2011-06-24T14:51:07Z Indexed on 2011/06/24 16:22 UTC
Read the original article Hit count: 171

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about url