2011-01-29 4 views

답변

0

HttpRequestInterceptor 클래스를 인증에 사용해야합니다.

는 여기에 내가 질문 오래 알고 있지만 이것에 걸림돌 사람의 이익 (내가했던 것처럼)을 위해, 당신은 HttpGet 개체와 헤더 직접 롤백 할 수 있습니다 예를 들어

HttpRequestInterceptor httpRequestInterceptor = new HttpRequestInterceptor() { 
    public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { 
     AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); 
     CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
       ClientContext.CREDS_PROVIDER); 
     HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); 

     if (authState.getAuthScheme() == null) { 
      AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); 
      Credentials creds = credsProvider.getCredentials(authScope); 
      if (creds != null) { 
       authState.setAuthScheme(new BasicScheme()); 
       authState.setCredentials(creds); 
      } 
     } 
    }  
}; 
+0

이렇게하면 새로운 질문 인 AndroidHttpClient에 인터셉터를 추가하는 방법이 열립니다. – lichtzeichenanlage

+0

왜 'AndroidHttpClient'를 사용하려고하는지 알려주십시오. –

+0

안녕하세요. 한편으로 코드는 나에게 깨끗해 보이지만, 다른 한편으로는 필자는 한 번 쓰고 유지하는 대신 표준 클래스를 사용하려고한다. 그 이유가 유효합니까? – lichtzeichenanlage

0

입니다. 마찬가지로 :

httpGet.addHeader("Authorization", "Basic " + Base64.encode(username+":"+password)); 
+0

형식에서 encode (byte [], int) 메서드를 참조하십시오. Base64는 인수 (문자열)에 적용 할 수 없습니다. –

0

다음의 코드는 Saad Farooq의 대답에 약간의 향상이 있습니다.

관련 문제