簡體   English   中英

Linux Mac上的Java URLConnection

[英]Java URLConnection on linux mac

我有以下代碼

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



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

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

當我打開此窗口時,它工作正常,但是當我在Mac / Linux中打開此窗口時,出現以下錯誤

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

任何想法如何使其在linux / mac中工作?

謝謝

問題可能在於,在Windows上,您將使用Windows登錄自動驗證到該站點的身份(我不知道這是如何工作的),但是在Linux或Mac上,至少在默認情況下,您必須自己執行此操作。

您公司的服務台可能會設置您的Linux或Mac計算機以相同的方式進行身份驗證...,但是當然只對那些特定的計算機有效。 可能需要一個更通用的解決方案。

我用了BasicAuthenticator

class BasicAuthenticator擴展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());
  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM