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
650468c0
Commit
650468c0
authored
Jan 22, 2017
by
xbynet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
解决POST中文参数乱码问题
parent
1f85674a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
5 deletions
+37
-5
Request.java
...gic-core/src/main/java/us/codecraft/webmagic/Request.java
+19
-0
HttpClientDownloader.java
...s/codecraft/webmagic/downloader/HttpClientDownloader.java
+18
-5
No files found.
webmagic-core/src/main/java/us/codecraft/webmagic/Request.java
View file @
650468c0
...
@@ -29,6 +29,10 @@ public class Request implements Serializable {
...
@@ -29,6 +29,10 @@ public class Request implements Serializable {
* Store additional information in extras.
* Store additional information in extras.
*/
*/
private
Map
<
String
,
Object
>
extras
;
private
Map
<
String
,
Object
>
extras
;
/**
* POST/GET param set
* */
private
Map
<
String
,
String
>
params
=
new
HashMap
<
String
,
String
>();
/**
/**
* Priority of the request.<br>
* Priority of the request.<br>
...
@@ -124,6 +128,21 @@ public class Request implements Serializable {
...
@@ -124,6 +128,21 @@ public class Request implements Serializable {
this
.
method
=
method
;
this
.
method
=
method
;
}
}
public
Map
<
String
,
String
>
getParams
()
{
return
params
;
}
/**
* POST/GET参数设置
* */
public
void
setParams
(
Map
<
String
,
String
>
params
)
{
this
.
params
=
params
;
}
/**
* POST/GET参数设置
* */
public
void
putParams
(
String
key
,
String
value
)
{
params
.
put
(
key
,
value
);
}
@Override
@Override
public
String
toString
()
{
public
String
toString
()
{
return
"Request{"
+
return
"Request{"
+
...
...
webmagic-core/src/main/java/us/codecraft/webmagic/downloader/HttpClientDownloader.java
View file @
650468c0
...
@@ -8,10 +8,12 @@ import org.apache.http.NameValuePair;
...
@@ -8,10 +8,12 @@ import org.apache.http.NameValuePair;
import
org.apache.http.annotation.ThreadSafe
;
import
org.apache.http.annotation.ThreadSafe
;
import
org.apache.http.client.config.CookieSpecs
;
import
org.apache.http.client.config.CookieSpecs
;
import
org.apache.http.client.config.RequestConfig
;
import
org.apache.http.client.config.RequestConfig
;
import
org.apache.http.client.entity.UrlEncodedFormEntity
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpUriRequest
;
import
org.apache.http.client.methods.HttpUriRequest
;
import
org.apache.http.client.methods.RequestBuilder
;
import
org.apache.http.client.methods.RequestBuilder
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.message.BasicNameValuePair
;
import
org.apache.http.util.EntityUtils
;
import
org.apache.http.util.EntityUtils
;
import
org.jsoup.Jsoup
;
import
org.jsoup.Jsoup
;
import
org.jsoup.nodes.Document
;
import
org.jsoup.nodes.Document
;
...
@@ -31,9 +33,7 @@ import us.codecraft.webmagic.utils.WMCollections;
...
@@ -31,9 +33,7 @@ import us.codecraft.webmagic.utils.WMCollections;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.nio.charset.Charset
;
import
java.nio.charset.Charset
;
import
java.util.HashMap
;
import
java.util.*
;
import
java.util.Map
;
import
java.util.Set
;
/**
/**
...
@@ -167,13 +167,26 @@ public class HttpClientDownloader extends AbstractDownloader {
...
@@ -167,13 +167,26 @@ public class HttpClientDownloader extends AbstractDownloader {
String
method
=
request
.
getMethod
();
String
method
=
request
.
getMethod
();
if
(
method
==
null
||
method
.
equalsIgnoreCase
(
HttpConstant
.
Method
.
GET
))
{
if
(
method
==
null
||
method
.
equalsIgnoreCase
(
HttpConstant
.
Method
.
GET
))
{
//default get
//default get
return
RequestBuilder
.
get
();
RequestBuilder
requestBuilder
=
RequestBuilder
.
get
();
if
(
request
.
getParams
()
!=
null
)
{
for
(
Map
.
Entry
<
String
,
String
>
entry
:
request
.
getParams
().
entrySet
())
{
requestBuilder
.
addParameter
(
entry
.
getKey
(),
entry
.
getValue
());
}
}
return
requestBuilder
;
}
else
if
(
method
.
equalsIgnoreCase
(
HttpConstant
.
Method
.
POST
))
{
}
else
if
(
method
.
equalsIgnoreCase
(
HttpConstant
.
Method
.
POST
))
{
RequestBuilder
requestBuilder
=
RequestBuilder
.
post
();
RequestBuilder
requestBuilder
=
RequestBuilder
.
post
();
NameValuePair
[]
nameValuePair
=
(
NameValuePair
[])
request
.
getExtra
(
"nameValuePair"
);
NameValuePair
[]
nameValuePair
=
(
NameValuePair
[])
request
.
getExtra
(
"nameValuePair"
);
List
<
NameValuePair
>
allNameValuePair
=
new
ArrayList
<
NameValuePair
>();
if
(
nameValuePair
!=
null
&&
nameValuePair
.
length
>
0
)
{
if
(
nameValuePair
!=
null
&&
nameValuePair
.
length
>
0
)
{
requestBuilder
.
addParameters
(
nameValuePair
);
allNameValuePair
=
Arrays
.
asList
(
nameValuePair
);
}
if
(
request
.
getParams
()
!=
null
)
{
for
(
String
key
:
request
.
getParams
().
keySet
())
{
allNameValuePair
.
add
(
new
BasicNameValuePair
(
key
,
request
.
getParams
().
get
(
key
)));
}
}
}
requestBuilder
.
setEntity
(
new
UrlEncodedFormEntity
(
allNameValuePair
,
Charset
.
forName
(
"utf8"
)));
return
requestBuilder
;
return
requestBuilder
;
}
else
if
(
method
.
equalsIgnoreCase
(
HttpConstant
.
Method
.
HEAD
))
{
}
else
if
(
method
.
equalsIgnoreCase
(
HttpConstant
.
Method
.
HEAD
))
{
return
RequestBuilder
.
head
();
return
RequestBuilder
.
head
();
...
...
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