2013-01-17 7 views
2

인증이 필요한 웹 캐스트 Socialcast에서 컨텐츠를 가져 오려고합니다. (먼저 기본 인증이있는 HTTP 게시를 수행 한 다음 HTTP GET을 시도합니다.) 이

[email protected] : 데모 Base64로 인코딩 된 인증 문자열 : ZW1pbHlAc29jaWFsY2FzdC5jb206ZGVtbw의 == * 당신은 redirected되고있다 을 BEGIN 은 내가 "결과"로이 수신 몇 가지 코드를 시도했다. HTTP 기본 인증 Java

가 여기에 HTTP 기본 인증을위한 코드 * END : 나는 나중에는 GET을 수행하려고 할 때

try { 
     String webPage = "http://demo.socialcast.com"; 
     String name = "[email protected]"; 
     String password = "demo"; 

     String authString = name + ":" + password; 
     System.out.println("auth string: " + authString); 
     byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); 
     String authStringEnc = new String(authEncBytes); 
     System.out.println("Base64 encoded auth string: " + authStringEnc); 

     URL url = new URL(webPage); 
     URLConnection urlConnection = url.openConnection(); 
     urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc); 
     InputStream is = urlConnection.getInputStream(); 
     InputStreamReader isr = new InputStreamReader(is); 

     int numCharsRead; 
     char[] charArray = new char[1024]; 
     StringBuffer sb = new StringBuffer(); 
     while ((numCharsRead = isr.read(charArray)) > 0) { 
      sb.append(charArray, 0, numCharsRead); 
     } 
     String result = sb.toString(); 

     System.out.println("*** BEGIN ***"); 
     System.out.println(result); 
     System.out.println("*** END ***"); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

은 그러나, 무단 말한다. 자격 증명은 [email protected]/demo입니다. 즉, 나 자신의 Socialcast 인스턴스에 액세스 할 수 없기 때문에 Socialcast Dev가 제공합니다.
이 코드가 잘못 되었습니까? 어떻게 제대로 할 수 있습니까? BTW, 나는 HttpClient 4.x를 사용하고있다.

답변

2

각 요청에 자격 증명을 보내시겠습니까? 나는 이것이 필요하다고 생각한다. 그렇지 않으면 서버가 다른 페이지를 볼 수있는 권한이 있음을 증명할 수있는 다른 정보가 없다. ...

+0

예. 하지만 첫 번째 경우에도 작동하지 않습니다 ... –

+0

HTTPS 페이지로 리디렉션 된 것 같습니다. 직접 쿼리를 시도 했습니까? –

+0

좋은 힌트, 나는 시도 할 것이다! –

0

예제 코드가없는 경우 왜이 질문에 태그가 붙을지 잘 모르겠다. 그것을 사용하지 마십시오. 사실, httpclient를 사용하면 인증을 쉽게 처리 할 수 ​​있습니다 (see here for the excellent tutorial).

+0

이 힌트를 보내 주셔서 감사합니다! –