Skip to content

Commit bc116a5

Browse files
committed
Merge pull request android-async-http#58 from apersaud/master
Support for BasicAuth using UsernamePasswordsCredentials
2 parents d144f41 + 7e9e60c commit bc116a5

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public void process(HttpRequest request, HttpContext context) {
138138
request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
139139
}
140140
for (String header : clientHeaderMap.keySet()) {
141-
request.addHeader(header, clientHeaderMap.get(header));
141+
request.addHeader(header, clientHeaderMap.get(header));
142142
}
143143
}
144144
});
@@ -239,7 +239,31 @@ public void setSSLSocketFactory(SSLSocketFactory sslSocketFactory) {
239239
* @param value the contents of the header
240240
*/
241241
public void addHeader(String header, String value) {
242-
clientHeaderMap.put(header, value);
242+
clientHeaderMap.put(header, value);
243+
}
244+
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);
243267
}
244268

245269
/**

0 commit comments

Comments
 (0)