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
395396c6
Commit
395396c6
authored
Apr 08, 2017
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加HttpRequestBody
parent
25df6650
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
69 additions
and
0 deletions
+69
-0
HttpRequestBody.java
...ain/java/us/codecraft/webmagic/model/HttpRequestBody.java
+69
-0
No files found.
webmagic-core/src/main/java/us/codecraft/webmagic/model/HttpRequestBody.java
0 → 100644
View file @
395396c6
package
us
.
codecraft
.
webmagic
.
model
;
import
org.apache.http.NameValuePair
;
import
org.apache.http.client.utils.URLEncodedUtils
;
import
org.apache.http.message.BasicNameValuePair
;
import
java.io.UnsupportedEncodingException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
/**
* @author code4crafter@gmail.com
* Date: 17/4/8
*/
public
class
HttpRequestBody
{
public
static
abstract
class
ContentType
{
public
static
final
String
JSON
=
"application/json"
;
public
static
final
String
XML
=
"text/xml"
;
public
static
final
String
FORM
=
"application/x-www-form-urlencoded"
;
public
static
final
String
MULTIPART
=
"multipart/form-data"
;
}
private
final
byte
[]
body
;
private
final
String
contentType
;
private
final
String
encoding
;
public
HttpRequestBody
(
byte
[]
body
,
String
contentType
,
String
encoding
)
{
this
.
body
=
body
;
this
.
contentType
=
contentType
;
this
.
encoding
=
encoding
;
}
public
String
getContentType
()
{
return
contentType
;
}
public
String
getEncoding
()
{
return
encoding
;
}
public
static
HttpRequestBody
json
(
String
json
,
String
encoding
)
throws
UnsupportedEncodingException
{
return
new
HttpRequestBody
(
json
.
getBytes
(
encoding
),
ContentType
.
JSON
,
encoding
);
}
public
static
HttpRequestBody
xml
(
String
xml
,
String
encoding
)
throws
UnsupportedEncodingException
{
return
new
HttpRequestBody
(
xml
.
getBytes
(
encoding
),
ContentType
.
XML
,
encoding
);
}
public
static
HttpRequestBody
custom
(
byte
[]
body
,
String
contentType
,
String
encoding
)
throws
UnsupportedEncodingException
{
return
new
HttpRequestBody
(
body
,
contentType
,
encoding
);
}
public
static
HttpRequestBody
form
(
Map
<
String
,
Object
>
params
,
String
encoding
)
throws
UnsupportedEncodingException
{
List
<
NameValuePair
>
nameValuePairs
=
new
ArrayList
<
NameValuePair
>(
params
.
size
());
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
params
.
entrySet
())
{
nameValuePairs
.
add
(
new
BasicNameValuePair
(
entry
.
getKey
(),
String
.
valueOf
(
entry
.
getValue
())));
}
return
new
HttpRequestBody
(
URLEncodedUtils
.
format
(
nameValuePairs
,
encoding
).
getBytes
(
encoding
),
ContentType
.
FORM
,
encoding
);
}
}
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