Skip to content

Commit 8b2a419

Browse files
committed
Add proxy basic auth support to ajaxcallback.
1 parent e178d1e commit 8b2a419

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/com/androidquery/callback/AbstractAjaxCallback.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,28 @@ public K proxy(String host, int port){
470470
return self();
471471
}
472472

473+
public K proxy(String host, int port, String user, String password){
474+
475+
proxy(host, port);
476+
477+
String authHeader = makeAuthHeader(user, password);
478+
return header("Proxy-Authorization", authHeader);
479+
480+
481+
}
482+
483+
private static String makeAuthHeader(String username, String password){
484+
485+
String cred = username + ":" + password;
486+
byte[] data = cred.getBytes();
487+
488+
String auth = "Basic " + new String(AQUtility.encode64(data, 0, data.length));
489+
490+
return auth;
491+
492+
}
493+
494+
473495
public K targetFile(File file){
474496
this.targetFile = file;
475497
return self();

tests/src/com/androidquery/test/AQueryAsyncTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ public void callback(String url, String json, AjaxStatus status) {
10851085
public void testAjaxProxy() throws ClientProtocolException, IOException{
10861086

10871087
String url = "http://www.google.com";
1088-
1088+
10891089
aq.ajax(url, String.class, new AjaxCallback<String>() {
10901090

10911091
@Override
@@ -1108,6 +1108,30 @@ public void callback(String url, String json, AjaxStatus status) {
11081108

11091109
}
11101110

1111+
public void testAjaxProxyBasicCredential() throws ClientProtocolException, IOException{
1112+
1113+
String url = "http://www.google.com";
1114+
String host = "192.168.1.6";
1115+
int port = 8081;
1116+
String user = "Peter";
1117+
String password = "orange99";
1118+
1119+
aq.ajax(url, String.class, new AjaxCallback<String>() {
1120+
1121+
@Override
1122+
public void callback(String url, String json, AjaxStatus status) {
1123+
1124+
done(url, json, status);
1125+
1126+
}
1127+
}.proxy(host, port, user, password));
1128+
1129+
waitAsync();
1130+
1131+
assertNotNull(result);
1132+
1133+
1134+
}
11111135

11121136
public void testAjaxXmlPullParser(){
11131137

@@ -1776,4 +1800,8 @@ public void callback(String url, JSONObject jo, AjaxStatus status) {
17761800
assertEquals(AjaxStatus.NETWORK_ERROR, status.getCode());
17771801

17781802
}
1803+
1804+
1805+
1806+
17791807
}

0 commit comments

Comments
 (0)