From 1eee4663cdfe4303118f8f5bda7c07e283a0ad4f Mon Sep 17 00:00:00 2001 From: Anthony Persaud Date: Wed, 6 Jun 2012 23:51:32 -0700 Subject: [PATCH 1/2] Added support for HTTP BasicAuth in requests --- .../loopj/android/http/AsyncHttpClient.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/com/loopj/android/http/AsyncHttpClient.java b/src/com/loopj/android/http/AsyncHttpClient.java index ff23ce712..89d0abbdc 100644 --- a/src/com/loopj/android/http/AsyncHttpClient.java +++ b/src/com/loopj/android/http/AsyncHttpClient.java @@ -242,6 +242,30 @@ public void addHeader(String header, String value) { clientHeaderMap.put(header, value); } + /** + * Sets basic authentication for the request. Uses AuthScope.ANY. This is the same as + * setBasicAuth('username','password',AuthScope.ANY) + * @param username + * @param password + */ + public void setBasicAuth(String user, String pass){ + AuthScope scope = AuthScope.ANY; + setBasicAuth(user, pass, scope); + } + + /** + * Sets basic authentication for the request. You should pass in your AuthScope for security. It should be like this + * setBasicAuth("username","password", new AuthScope("host",port,AuthScope.ANY_REALM)) + * @param username + * @param password + * @param scope - an AuthScope object + * + */ + public void setBasicAuth( String user, String pass, AuthScope scope){ + UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user,pass); + this.httpClient.getCredentialsProvider().setCredentials(scope, credentials); + } + /** * Cancels any pending (or potentially active) requests associated with the * passed Context. From 7e9e60cd634d2bcde02ecef82314ef8d552fff41 Mon Sep 17 00:00:00 2001 From: Anthony Persaud Date: Wed, 6 Jun 2012 23:55:15 -0700 Subject: [PATCH 2/2] fixed indentation --- .../loopj/android/http/AsyncHttpClient.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/com/loopj/android/http/AsyncHttpClient.java b/src/com/loopj/android/http/AsyncHttpClient.java index 89d0abbdc..49ae15344 100644 --- a/src/com/loopj/android/http/AsyncHttpClient.java +++ b/src/com/loopj/android/http/AsyncHttpClient.java @@ -138,7 +138,7 @@ public void process(HttpRequest request, HttpContext context) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } for (String header : clientHeaderMap.keySet()) { - request.addHeader(header, clientHeaderMap.get(header)); + request.addHeader(header, clientHeaderMap.get(header)); } } }); @@ -239,31 +239,31 @@ public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory) { * @param value the contents of the header */ public void addHeader(String header, String value) { - clientHeaderMap.put(header, value); + clientHeaderMap.put(header, value); } /** * Sets basic authentication for the request. Uses AuthScope.ANY. This is the same as - * setBasicAuth('username','password',AuthScope.ANY) + * setBasicAuth('username','password',AuthScope.ANY) * @param username * @param password */ public void setBasicAuth(String user, String pass){ - AuthScope scope = AuthScope.ANY; - setBasicAuth(user, pass, scope); + AuthScope scope = AuthScope.ANY; + setBasicAuth(user, pass, scope); } /** * Sets basic authentication for the request. You should pass in your AuthScope for security. It should be like this - * setBasicAuth("username","password", new AuthScope("host",port,AuthScope.ANY_REALM)) + * setBasicAuth("username","password", new AuthScope("host",port,AuthScope.ANY_REALM)) * @param username * @param password - * @param scope - an AuthScope object - * + * @param scope - an AuthScope object + * */ public void setBasicAuth( String user, String pass, AuthScope scope){ - UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user,pass); - this.httpClient.getCredentialsProvider().setCredentials(scope, credentials); + UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user,pass); + this.httpClient.getCredentialsProvider().setCredentials(scope, credentials); } /**