Skip to content

Commit 3ee0001

Browse files
committed
change header from Authorization to Proxy-Authorization for Proxy Authorization #596
1 parent 4111b07 commit 3ee0001

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

webmagic-core/src/main/java/us/codecraft/webmagic/downloader/HttpUriRequestConverter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.apache.http.HttpHost;
44
import org.apache.http.auth.AuthState;
5+
import org.apache.http.auth.ChallengeState;
56
import org.apache.http.auth.UsernamePasswordCredentials;
67
import org.apache.http.client.CookieStore;
78
import org.apache.http.client.config.CookieSpecs;
@@ -41,7 +42,7 @@ private HttpClientContext convertHttpClientContext(Request request, Site site, P
4142
HttpClientContext httpContext = new HttpClientContext();
4243
if (proxy != null && proxy.getUsername() != null) {
4344
AuthState authState = new AuthState();
44-
authState.update(new BasicScheme(), new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()));
45+
authState.update(new BasicScheme(ChallengeState.PROXY), new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()));
4546
httpContext.setAttribute(HttpClientContext.PROXY_AUTH_STATE, authState);
4647
}
4748
if (request.getCookies() != null && !request.getCookies().isEmpty()) {

webmagic-core/src/test/java/us/codecraft/webmagic/downloader/HttpClientDownloaderTest.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import org.apache.http.impl.client.CloseableHttpClient;
1111
import org.apache.http.impl.client.HttpClients;
1212
import org.apache.http.util.EntityUtils;
13-
import org.junit.Ignore;
1413
import org.junit.Test;
1514
import us.codecraft.webmagic.Page;
1615
import us.codecraft.webmagic.Request;
@@ -255,15 +254,21 @@ public void run() throws Exception {
255254
});
256255
}
257256

258-
@Ignore("need proxy server")
259257
@Test
260-
public void test_download_by_SimpleProxyProvider(){
261-
HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
262-
httpClientDownloader.setProxyProvider(SimpleProxyProvider.from(new Proxy("127.0.0.1", 1087)));
263-
Request request = new Request();
264-
request.setUrl("https://www.baidu.com");
265-
Page page = httpClientDownloader.download(request, Site.me().toTask());
266-
assertThat(page.isDownloadSuccess());
258+
public void test_download_auth_by_SimpleProxyProvider() throws Exception {
259+
HttpServer server = httpServer(13423);
260+
server.get(eq(header("Proxy-Authorization"), "Basic dXNlcm5hbWU6cGFzc3dvcmQ=")).response("ok");
261+
Runner.running(server, new Runnable() {
262+
@Override
263+
public void run() throws Exception {
264+
HttpClientDownloader httpClientDownloader = new HttpClientDownloader();
265+
httpClientDownloader.setProxyProvider(SimpleProxyProvider.from(new Proxy("127.0.0.1", 13423, "username", "password")));
266+
Request request = new Request();
267+
request.setUrl("http://www.baidu.com");
268+
Page page = httpClientDownloader.download(request, Site.me().toTask());
269+
assertThat(page.getRawText()).isEqualTo("ok");
270+
}
271+
});
267272
}
268273

269274
}

0 commit comments

Comments
 (0)