Skip to content

Commit 9de64ea

Browse files
committed
Merge branch 'hepan-master'
2 parents d8f978f + b7f3c4b commit 9de64ea

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

webmagic-core/src/main/java/us/codecraft/webmagic/Site.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import us.codecraft.webmagic.proxy.Proxy;
88
import us.codecraft.webmagic.proxy.SimpleProxyPool;
9+
import org.apache.http.auth.UsernamePasswordCredentials;
910
import us.codecraft.webmagic.proxy.ProxyPool;
1011
import us.codecraft.webmagic.utils.UrlUtils;
1112

@@ -53,6 +54,8 @@ public class Site {
5354

5455
private HttpHost httpProxy;
5556

57+
private UsernamePasswordCredentials usernamePasswordCredentials; //代理用户名密码设置
58+
5659
private ProxyPool httpProxyPool;
5760

5861
private boolean useGzip = true;
@@ -486,6 +489,15 @@ public Site enableHttpProxyPool() {
486489
return this;
487490
}
488491

492+
public UsernamePasswordCredentials getUsernamePasswordCredentials() {
493+
return usernamePasswordCredentials;
494+
}
495+
496+
public Site setUsernamePasswordCredentials(UsernamePasswordCredentials usernamePasswordCredentials) {
497+
this.usernamePasswordCredentials = usernamePasswordCredentials;
498+
return this;
499+
}
500+
489501
public ProxyPool getHttpProxyPool() {
490502
return httpProxyPool;
491503
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ public void process(
8181
}
8282
});
8383
}
84+
85+
if(site!=null&&site.getHttpProxy()!=null&&site.getUsernamePasswordCredentials()!=null){
86+
CredentialsProvider credsProvider = new BasicCredentialsProvider();
87+
credsProvider.setCredentials(
88+
new AuthScope(site.getHttpProxy()),//可以访问的范围
89+
site.getUsernamePasswordCredentials());//用户名和密码
90+
httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
91+
}
92+
8493
SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build();
8594
httpClientBuilder.setDefaultSocketConfig(socketConfig);
8695
if (site != null) {

0 commit comments

Comments
 (0)