2

Hi I have something like the following that makes a URL Connection and retrieves the data. The problem is that when I hit a page that requires authentication first I get the login page. I'm unsure of how to pass a username and password in the URLConnection. I'm hitting a site that is using JAAS authenticaiton.

import java.net.URL;
import java.net.URLConnection;

.....

URL location = new URL("www.sampleURL.com");
URLConnection yc = location.openConnection();
BufferedReader in = new BufferedReader(
    new InputStreamReader(
    yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) 
    data += inputLine + "\n";
in.close();

I tried something like this but it didn't seem to work...

URL url = new URL("www.myURL.com");
URLConnection connection = url.openConnection();
String login = "username:password";
String encodedLogin = new BASE64Encoder().encodeBuffer(login.getBytes());
connection.setRequestProperty("Proxy-Authorization", "Basic " + encodedLogin);
Matteo
  • 14,696
  • 9
  • 68
  • 106

1 Answers1

6

Instead of "Proxy-Authorization", try "Authorization". The "Proxy-Authorization" header is used when authenticating against a proxy rather than the actual web server you are trying to access.

monceaux
  • 596
  • 2
  • 5