2014-05-19 1 views
0

간단한 java 프로그램에서 http에서 파일을 가져 오려고합니다. 도메인 사용자 이름과 로컬 사용자 이름을 모두 사용하려고했습니다. 우리는 Windows Server 2003을 사용하고 있습니다.암호로 보호 된 http에서 정보를 가져 오는 중입니다. Java

두 가지 해결책이 있습니다. 첫 번째는 : 내가 찾은

public class HttpTest { 

    public static void main(String[] args) { 
     try { 

      URL url = new URL("http://corp.domain.com/name/info.html"); 
      String userPassword = "user:password"; 
      String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes()); 
      CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); 
      URLConnection connection = url.openConnection(); 
      connection.setRequestProperty("Authorization", "Basic " + encoding); 
      InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 
      BufferedReader in = new BufferedReader(isr); 
      String str; 
      while ((str = in.readLine()) != null) { 
       System.out.println(str); 
      } 
      in.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

다른 방법은 다음과 같습니다

java.io.IOException: Server returned HTTP response code: 401 for URL: http://corp.domain.com/name/info.html 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1838) 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439) 
at httptest.HttpTest.main(HttpTest.java:51) 

라인 51 점 :

InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 

public class HttpTest { 

    public static void main(String[] args) { 
     try { 
      Authenticator.setDefault(new Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       String prompt = getRequestingPrompt(); 
       String host = getRequestingHost(); 
       InetAddress addressIP = getRequestingSite(); 
       int port = getRequestingPort(); 
       String username = "user"; 
       String password = "password"; 

       return new PasswordAuthentication(username, password.toCharArray()); 
      } 
     }); 

      URL url = new URL("http://corp.domain.com/name/info.html"); 
      CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL)); 
      URLConnection connection = url.openConnection(); 
      InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 
      BufferedReader in = new BufferedReader(isr); 
      String str; 
      while ((str = in.readLine()) != null) { 
       System.out.println(str); 
      } 
      in.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

첫 번째는 다음과 같은 예외가 발생합니다

두 번째 사람은 다음을 throw합니다.

java.net.ProtocolException: Server redirected too many times (20) 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1846) 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439) 
at httptest.HttpTest.main(HttpTest.java:51) 

51 번째 줄은 동일합니다.

브라우저에서 서버에 액세스하려고하면 로그인 암호 쌍 (도메인 및 로컬)이 모두 작동합니다.

도움을 주시면 감사하겠습니다.

+0

확실 서버가 기본 인증을 사용하고 있습니까이? IIS에는 몇 가지 모드가 있습니다. http://msdn.microsoft.com/en-us/library/ee825205(v=cs.10).aspx – Zeki

+0

저는 아닙니다. 이런 일을 처음 시도하기 때문에. – user3507225

+0

Hmm .. 브라우저에서 보낸 실제 헤더를 보려면 브라우저에서 네트워크 속성이나 다른 것을 사용할 수 있습니까? 파이어 폭스 또는 크롬 – Zeki

답변

관련 문제