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
5a6a68a3
Commit
5a6a68a3
authored
Jun 23, 2013
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add gzip support
parent
adeed3bc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
1 deletion
+21
-1
HttpClientDownloader.java
...s/codecraft/webmagic/downloader/HttpClientDownloader.java
+21
-1
No files found.
webmagic-core/src/main/java/us/codecraft/webmagic/downloader/HttpClientDownloader.java
View file @
5a6a68a3
package
us
.
codecraft
.
webmagic
.
downloader
;
package
us
.
codecraft
.
webmagic
.
downloader
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.http.Header
;
import
org.apache.http.HeaderElement
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.client.HttpClient
;
import
org.apache.http.client.HttpClient
;
import
org.apache.http.client.entity.GzipDecompressingEntity
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.log4j.Logger
;
import
org.apache.log4j.Logger
;
import
us.codecraft.webmagic.Page
;
import
us.codecraft.webmagic.Page
;
...
@@ -26,15 +29,19 @@ public class HttpClientDownloader implements Downloader {
...
@@ -26,15 +29,19 @@ public class HttpClientDownloader implements Downloader {
public
Page
download
(
Request
request
,
Site
site
)
{
public
Page
download
(
Request
request
,
Site
site
)
{
logger
.
info
(
"downloading page "
+
request
.
getUrl
());
logger
.
info
(
"downloading page "
+
request
.
getUrl
());
HttpClient
httpClient
=
HttpClientPool
.
getInstance
().
getClient
(
site
);
HttpClient
httpClient
=
HttpClientPool
.
getInstance
().
getClient
(
site
);
String
encoding
=
site
.
getEncoding
();
try
{
try
{
HttpGet
httpGet
=
new
HttpGet
(
request
.
getUrl
());
HttpGet
httpGet
=
new
HttpGet
(
request
.
getUrl
());
HttpResponse
httpResponse
=
httpClient
.
execute
(
httpGet
);
HttpResponse
httpResponse
=
httpClient
.
execute
(
httpGet
);
int
statusCode
=
httpResponse
.
getStatusLine
().
getStatusCode
();
int
statusCode
=
httpResponse
.
getStatusLine
().
getStatusCode
();
if
(
site
.
getAcceptStatCode
().
contains
(
statusCode
))
{
if
(
site
.
getAcceptStatCode
().
contains
(
statusCode
))
{
if
(
site
.
getEncoding
()
==
null
){
//charset
if
(
encoding
==
null
){
String
value
=
httpResponse
.
getEntity
().
getContentType
().
getValue
();
String
value
=
httpResponse
.
getEntity
().
getContentType
().
getValue
();
site
.
setEncoding
(
new
PlainText
(
value
).
regex
(
"charset=([^\\s]+)"
).
toString
());
site
.
setEncoding
(
new
PlainText
(
value
).
regex
(
"charset=([^\\s]+)"
).
toString
());
}
}
//
handleGzip
(
httpResponse
);
String
content
=
IOUtils
.
toString
(
httpResponse
.
getEntity
().
getContent
(),
String
content
=
IOUtils
.
toString
(
httpResponse
.
getEntity
().
getContent
(),
site
.
getEncoding
());
site
.
getEncoding
());
Page
page
=
new
Page
();
Page
page
=
new
Page
();
...
@@ -50,4 +57,17 @@ public class HttpClientDownloader implements Downloader {
...
@@ -50,4 +57,17 @@ public class HttpClientDownloader implements Downloader {
}
}
return
null
;
return
null
;
}
}
private
void
handleGzip
(
HttpResponse
httpResponse
)
{
Header
ceheader
=
httpResponse
.
getEntity
().
getContentEncoding
();
if
(
ceheader
!=
null
)
{
HeaderElement
[]
codecs
=
ceheader
.
getElements
();
for
(
int
i
=
0
;
i
<
codecs
.
length
;
i
++)
{
if
(
codecs
[
i
].
getName
().
equalsIgnoreCase
(
"gzip"
))
{
httpResponse
.
setEntity
(
new
GzipDecompressingEntity
(
httpResponse
.
getEntity
()));
}
}
}
}
}
}
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