HTTP authentication

R

Randy

Help!

I can't seem to get this code to work. I have found numerous sites
that show the same code that I am using.

I am using jdk 1.5 and trying to generate the
"Authorization: Basic erwerwqerwerwer" line in the http get header. I
have tried the "Authenticator.setDefault (new MyAuthenticator ());"
route and it did not work. I have found a lot of web sites with the
code snippets

// Build the string to be used for Basic Authentication
<username>:<password>
String userPassword = "username:password";

// Base64 encode the authentication string
String encoding = new sun.misc.BASE64Encoder().encode
(userPassword.getBytes());

URL url = new URL ("http://www.server.com");
HttpURLConnection connection =
HttpURLConnection)url.openConnection();

connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Authorization", "Basic " +
encoding);
connection.connect();

BufferedReader in =
new BufferedReader (new InputStreamReader
((InputStream)url.getContent()));

String line;
while ((line = in.readLine()) != null) {
pw.println (line);
}



but none of this works. All I get is:

GET / HTTP/1.1
User-Agent: Java/1.4.2
Host: www.server.com
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive



Either the JDK does not work or I am doing something stupid. Help!
Please? I am about to just say screw it and use a raw socket and write
the get request manually.

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top