Question

I have following code

URL url = new URL("http://internalsite");



URLConnection conn = url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    String inputLine;
            System.out.println(conn);

When I opens this windows it works fine But when I opens this in Mac/ Linux I got following error

java.io.IOException: Server returned HTTP response code: 401 for URL: http://internalsite
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1459)

Any idea how to make it work in linux/Mac?

Thanks

Was it helpful?

Solution 2

I used BasicAuthenticator

class BasicAuthenticator extends Authenticator {

private  BasicAuthenticator() {
    // TODO Auto-generated constructor stub
}
public static BasicAuthenticator THIS = new BasicAuthenticator();

public static Authenticator getAutheticator(){
    return THIS;
}
  final static String USERNAME = "";
  final static String PASSWORD = "";

  @Override
  public PasswordAuthentication getPasswordAuthentication() {

    return new PasswordAuthentication(USERNAME, PASSWORD.toCharArray());
  }

}

OTHER TIPS

The problem is probably that on Windows, you are automatically authenticated to the site using your Windows logon (I don't know how this works), but on Linux or Mac you have to do this yourself, at least by default.

It might be possible for your company helpdesk to set up your Linux or Mac computers to authenticate in the same way... but of course that would only work for those particular computers. Probably a more general solution is needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top