IOExceptionjava.io.IOException: Server returned HTTP response code: 401

Hi,

I am trying to read a file from the remote http location using the following program.

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.Authenticator;

import java.net.MalformedURLException;

import java.net.PasswordAuthentication;

import java.net.URL;

import java.net.URLConnection;

public class ToDoList {

/**

* @param args

*/

static final String kuser = "myUserName"; // your account name

static final String kpass = "myPassword"; // your password for the account

static class MyAuthenticator extends Authenticator {

public PasswordAuthentication getPasswordAuthentication() {

// I haven't checked getRequestingScheme() here, since for NTLM

// and Negotiate, the usrname and password are all the same.

System.err.println("Feeding username and password for " + getRequestingScheme());

return (new PasswordAuthentication(kuser, kpass.toCharArray()));

}

}

public static void main(String[] args) {

System.out.println("Before Authenticator");

Authenticator.setDefault(new MyAuthenticator());

System.out.println("After Authenticator");

try {

URL url;

URLConnection urlConn;

url = new URL("http://erbidocs.tdc.cingular.net/CDC/CDC%20-%20Activation%20Notes.txt");

urlConn = url.openConnection();

urlConn.setDoInput(true);

urlConn.setUseCaches(false);

System.out.println("Before BufferedReader");

System.out.println("urlConn.getInputStream() : " + urlConn.getInputStream());

BufferedReader d = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

System.out.println("After BufferedReader");

String s;

System.out.println("Before");

while ((s = d.readLine()) != null) {

System.out.println(s);

}

System.out.println("After");

d.close();

} catch (MalformedURLException mue) {

System.out.println("IOException" + mue);

} catch (IOException ioe) {

System.out.println("IOException" + ioe);

}

}

}

This works well on the windows environment but when I run it on unix environment it gives me following error:

IOExceptionjava.io.IOException: Server returned HTTP response code: 401 for URL: http://erbidocs.tdc.cingular.net/CDC/CDC%20-%20Activation%20Notes.txt

Please advice, any help is appreciated.

Thank you,

Rajesh

[2528 byte] By [rmanea] at [2008-1-7]