2010-11-22 8 views
3

HTTP 인증 헤더를 보내는 방법을 모르겠습니다.Android에서 HTTP 기본 인증 헤더를 보내려면 어떻게해야합니까?

요청을 받기 위해 다음 HttpClient를 가지고 있는데 어떻게 요청을 보낼 수 있는지 잘 모르십니까?

public class RestClient extends AsyncTask<String, Void, JSONObject> { 
     private String convertStreamToString(InputStream is) { 
      /* 
      * To convert the InputStream to String we use the 
      * BufferedReader.readLine() method. We iterate until the 
      * BufferedReader return null which means there's no more data to 
      * read. Each line will appended to a StringBuilder and returned as 
      * String. 
      */ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
      StringBuilder sb = new StringBuilder(); 

      String line = null; 
      try { 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       try { 
        is.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 

      return sb.toString(); 
     } 

     /* 
     * This is a test function which will connects to a given rest service 
     * and prints it's response to Android Log with labels "Praeda". 
     */ 
     public JSONObject connect(String url) { 
      HttpClient httpclient = new DefaultHttpClient(); 

      // Prepare a request object 
      HttpGet httpget = new HttpGet(url); 

      // Execute the request 
      HttpResponse response; 
      try { 
       response = httpclient.execute(httpget); 
       // Examine the response status 
       Log.i("Praeda", response.getStatusLine().toString()); 

       // Get hold of the response entity 
       HttpEntity entity = response.getEntity(); 

       if (entity != null) { 

        // A Simple JSON Response Read 
        InputStream instream = entity.getContent(); 
        String result = convertStreamToString(instream); 

        // A Simple JSONObject Creation 
        JSONObject json = new JSONObject(result); 

        // Closing the input stream will trigger connection release 
        instream.close(); 

        return json; 
       } 

      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     @Override 
     protected JSONObject doInBackground(String... urls) { 
      return connect(urls[0]); 
     } 

     @Override 
     protected void onPostExecute(JSONObject json) { 

     } 
    } 

답변

6

이는 HttpClient를 documentation에 자신의 sample code에 덮여있다.

+10

설명서의 일부 방법은 Android 플랫폼에 존재하지 않습니다. –

0

어쩌면 HttpClient를의 문서는 도움이 될 수 있습니다 link

+2

FYI, 귀하의 링크는 HttpClient 3.x이고, Android는 HttpClient 4.x가 내장되어 있습니다. – CommonsWare

0

을 조각하여 참고 용이며 아래 안드로이드, 대신 3.X의 4.0.x의 컴파일 HttpClient를하기 때문에.

if (authState.getAuthScheme() == null) { 
     AuthScope authScope = new Au HttpRequestInterceptor preemptiveAuth = 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);thScope(targetHost.getHostName(), targetHost.getPort()); 
     Credentials creds = credsProvider.getCredentials(authScope); 
     if (creds != null) { 
      authState.setAuthScheme(new BasicScheme()); 
      authState.setCredentials(creds); 
     } 
    } 
}  
}; 
DefaultHttpClient httpclient = new DefaultHttpClient(); 
httpclient.addRequestInterceptor(preemptiveAuth, 0); 
관련 문제