Skip to content

Commit 1eee466

Browse files
committed
Added support for HTTP BasicAuth in requests
1 parent d144f41 commit 1eee466

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,30 @@ public void addHeader(String header, String value) {
242242
clientHeaderMap.put(header, value);
243243
}
244244

245+
/**
246+
* Sets basic authentication for the request. Uses AuthScope.ANY. This is the same as
247+
* setBasicAuth('username','password',AuthScope.ANY)
248+
* @param username
249+
* @param password
250+
*/
251+
public void setBasicAuth(String user, String pass){
252+
AuthScope scope = AuthScope.ANY;
253+
setBasicAuth(user, pass, scope);
254+
}
255+
256+
/**
257+
* Sets basic authentication for the request. You should pass in your AuthScope for security. It should be like this
258+
* setBasicAuth("username","password", new AuthScope("host",port,AuthScope.ANY_REALM))
259+
* @param username
260+
* @param password
261+
* @param scope - an AuthScope object
262+
*
263+
*/
264+
public void setBasicAuth( String user, String pass, AuthScope scope){
265+
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user,pass);
266+
this.httpClient.getCredentialsProvider().setCredentials(scope, credentials);
267+
}
268+
245269
/**
246270
* Cancels any pending (or potentially active) requests associated with the
247271
* passed Context.

0 commit comments

Comments
 (0)