File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed
webmagic-core/src/main/java/us/codecraft/webmagic Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change 66
77import us .codecraft .webmagic .proxy .Proxy ;
88import us .codecraft .webmagic .proxy .SimpleProxyPool ;
9+ import org .apache .http .auth .UsernamePasswordCredentials ;
910import us .codecraft .webmagic .proxy .ProxyPool ;
1011import 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 }
Original file line number Diff line number Diff 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 ) {
You can’t perform that action at this time.
0 commit comments