Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webmagic
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
沈俊林
webmagic
Commits
89c6e528
Commit
89c6e528
authored
Apr 13, 2016
by
hepan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
代理增加用户名密码认证
parent
047cb8ff
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
0 deletions
+24
-0
Site.java
webmagic-core/src/main/java/us/codecraft/webmagic/Site.java
+12
-0
HttpClientGenerator.java
...us/codecraft/webmagic/downloader/HttpClientGenerator.java
+12
-0
No files found.
webmagic-core/src/main/java/us/codecraft/webmagic/Site.java
View file @
89c6e528
...
@@ -4,6 +4,7 @@ import com.google.common.collect.HashBasedTable;
...
@@ -4,6 +4,7 @@ import com.google.common.collect.HashBasedTable;
import
com.google.common.collect.Table
;
import
com.google.common.collect.Table
;
import
org.apache.http.HttpHost
;
import
org.apache.http.HttpHost
;
import
org.apache.http.auth.UsernamePasswordCredentials
;
import
us.codecraft.webmagic.proxy.ProxyPool
;
import
us.codecraft.webmagic.proxy.ProxyPool
;
import
us.codecraft.webmagic.utils.UrlUtils
;
import
us.codecraft.webmagic.utils.UrlUtils
;
...
@@ -51,6 +52,8 @@ public class Site {
...
@@ -51,6 +52,8 @@ public class Site {
private
HttpHost
httpProxy
;
private
HttpHost
httpProxy
;
private
UsernamePasswordCredentials
usernamePasswordCredentials
;
//代理用户名密码设置
private
ProxyPool
httpProxyPool
;
private
ProxyPool
httpProxyPool
;
private
boolean
useGzip
=
true
;
private
boolean
useGzip
=
true
;
...
@@ -479,6 +482,15 @@ public class Site {
...
@@ -479,6 +482,15 @@ public class Site {
return
this
;
return
this
;
}
}
public
UsernamePasswordCredentials
getUsernamePasswordCredentials
()
{
return
usernamePasswordCredentials
;
}
public
Site
setUsernamePasswordCredentials
(
UsernamePasswordCredentials
usernamePasswordCredentials
)
{
this
.
usernamePasswordCredentials
=
usernamePasswordCredentials
;
return
this
;
}
public
ProxyPool
getHttpProxyPool
()
{
public
ProxyPool
getHttpProxyPool
()
{
return
httpProxyPool
;
return
httpProxyPool
;
}
}
...
...
webmagic-core/src/main/java/us/codecraft/webmagic/downloader/HttpClientGenerator.java
View file @
89c6e528
...
@@ -3,7 +3,10 @@ package us.codecraft.webmagic.downloader;
...
@@ -3,7 +3,10 @@ package us.codecraft.webmagic.downloader;
import
org.apache.http.HttpException
;
import
org.apache.http.HttpException
;
import
org.apache.http.HttpRequest
;
import
org.apache.http.HttpRequest
;
import
org.apache.http.HttpRequestInterceptor
;
import
org.apache.http.HttpRequestInterceptor
;
import
org.apache.http.auth.AuthScope
;
import
org.apache.http.auth.UsernamePasswordCredentials
;
import
org.apache.http.client.CookieStore
;
import
org.apache.http.client.CookieStore
;
import
org.apache.http.client.CredentialsProvider
;
import
org.apache.http.config.Registry
;
import
org.apache.http.config.Registry
;
import
org.apache.http.config.RegistryBuilder
;
import
org.apache.http.config.RegistryBuilder
;
import
org.apache.http.config.SocketConfig
;
import
org.apache.http.config.SocketConfig
;
...
@@ -65,6 +68,15 @@ public class HttpClientGenerator {
...
@@ -65,6 +68,15 @@ public class HttpClientGenerator {
}
}
});
});
}
}
if
(
site
!=
null
&&
site
.
getHttpProxy
()!=
null
&&
site
.
getUsernamePasswordCredentials
()!=
null
){
CredentialsProvider
credsProvider
=
new
BasicCredentialsProvider
();
credsProvider
.
setCredentials
(
new
AuthScope
(
site
.
getHttpProxy
()),
//可以访问的范围
site
.
getUsernamePasswordCredentials
());
//用户名和密码
httpClientBuilder
.
setDefaultCredentialsProvider
(
credsProvider
);
}
SocketConfig
socketConfig
=
SocketConfig
.
custom
().
setSoKeepAlive
(
true
).
setTcpNoDelay
(
true
).
build
();
SocketConfig
socketConfig
=
SocketConfig
.
custom
().
setSoKeepAlive
(
true
).
setTcpNoDelay
(
true
).
build
();
httpClientBuilder
.
setDefaultSocketConfig
(
socketConfig
);
httpClientBuilder
.
setDefaultSocketConfig
(
socketConfig
);
if
(
site
!=
null
)
{
if
(
site
!=
null
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment