2014-09-15 2 views
0

HTTP GET에서 작동하도록 기본 인증을 가져올 수 없습니다. getCredentialsProvider가 존재하지 않습니다. 그런 다음 HttpBuilder를 둘러 보았습니다.하지만 존재하지 않습니다. Android Sdk 업데이트를 실행해도 운이 좋지 않습니다. 3 시간을 보냈고 내가 찾은 모든 것을 시도했지만 항상 존재하지 않는 부분이있었습니다.기본 인증 Android API 20

HttpClient httpclient = new DefaultHttpClient(); 
String username = "User"; 
String password = "Pass"; 
Credentials credentials = new UsernamePasswordCredentials(username , password); 
httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY,credentials); 

감사

+0

답변을 모르겠지만 기본 인증을 사용하지 마십시오. 그것은 매우 불안합니다. 모든 장치가이를 깨뜨릴 수 있습니다. 대신 다이제스트를 사용하십시오! – JRsz

답변

1
Its worked for me. 

Authenticator.setDefault(new Authenticator(){ 
    @Override 
    protected PasswordAuthentication getPasswordAuthentication() { 
    return new PasswordAuthentication("xxx","xxx1234".toCharArray()); 
}}); 

HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
connection.setConnectTimeout(Integer.parseInt(context.getResources().getString(R.string.connection_timeout))); 
connection.setUseCaches(false); 
connection.connect(); 
HttpURLConnection httpConnection = connection; 
int responseCode = httpConnection.getResponseCode(); 
if (responseCode == HttpURLConnection.HTTP_OK) { 
    InputStream in = httpConnection.getInputStream(); 
} 
3

당신은 HttpConnection에를 사용은, URI에는 http://www.google.com 같은 것입니다 만, 김 HJ 말했듯이, 그 정말 불안 때문에 테스트 용으로 만 또는 고도의 보안 네트워크에서 사용. 그렇지 않으면 자격 증명을 공개 ;-) 있습니다

URL url = new URL(uri); 
HttpURLConnection httpRequest = (HttpURLConnection) url.openConnection(); 
httpRequest.setRequestMethod("GET"); 
httpRequest.setDoInput(true); 
String authString = username + ":" + password; 
byte[] authEncBytes = android.util.Base64.encode(authString.getBytes(), android.util.Base64.DEFAULT); 
String authStringEnc = new String(authEncBytes); 
httpsRequest.addRequestProperty("Authorization", "Basic "+ authStringEnc); 
+0

내가 비어있는 결과를 얻으려고하면 httpRequest.connect(); reader = 새 BufferedReader (새 InputStreamReader (httpRequest.getInputStream())); –

1

는 HTTP 기본 인증 할 수있는 두 가지 방법이 있습니다 : 헤더

HttpUriRequest 요청 = 새로운 HttpGet (YOUR_URL)에 추가

  1. 는;
    문자열 자격 증명 = YOUR_USERNAME + ":"+ YOUR_PASSWORD;
    String base64EncodedCredentials = Base64.encodeToString (credentials.getBytes(), Base64.NO_WRAP);
    request.addHeader ("Authorization", "Basic"+ base64EncodedCredentials);

  2. 자격증 명 공급자에게 자격 증명을 추가하십시오. .

    defaultHttpClient.getCredentialsProvider() 들어 setCredentials ( 새로운 AuthScope (AuthScope.ANY_HOST, AuthScope.ANY_PORT) 새로운 UsernamePasswordCredentials ( HTTPS_BASIC_AUTH_USERNAME, HTTPS_BASIC_AUTH_PASWORD));

희망이 있으면 도움이 될 것입니다.