View Javadoc
1   package us.codecraft.webmagic.model;
2   
3   import org.apache.http.NameValuePair;
4   import org.apache.http.client.utils.URLEncodedUtils;
5   import org.apache.http.message.BasicNameValuePair;
6   
7   import java.io.Serializable;
8   import java.io.UnsupportedEncodingException;
9   import java.util.ArrayList;
10  import java.util.List;
11  import java.util.Map;
12  
13  /**
14   * @author code4crafter@gmail.com
15   *         Date: 17/4/8
16   */
17  public class HttpRequestBody implements Serializable {
18  
19      private static final long serialVersionUID = 5659170945717023595L;
20  
21      public static abstract class ContentType {
22  
23          public static final String JSON = "application/json";
24  
25          public static final String XML = "text/xml";
26  
27          public static final String FORM = "application/x-www-form-urlencoded";
28  
29          public static final String MULTIPART = "multipart/form-data";
30      }
31  
32      private byte[] body;
33  
34      private String contentType;
35  
36      private String encoding;
37  
38      public HttpRequestBody() {
39      }
40  
41      public HttpRequestBody(byte[] body, String contentType, String encoding) {
42          this.body = body;
43          this.contentType = contentType;
44          this.encoding = encoding;
45      }
46  
47      public String getContentType() {
48          return contentType;
49      }
50  
51      public String getEncoding() {
52          return encoding;
53      }
54  
55      public void setBody(byte[] body) {
56          this.body = body;
57      }
58  
59      public void setContentType(String contentType) {
60          this.contentType = contentType;
61      }
62  
63      public void setEncoding(String encoding) {
64          this.encoding = encoding;
65      }
66  
67      public static HttpRequestBody json(String json, String encoding) {
68          try {
69              return new HttpRequestBody(json.getBytes(encoding), ContentType.JSON, encoding);
70          } catch (UnsupportedEncodingException e) {
71              throw new IllegalArgumentException("illegal encoding " + encoding, e);
72          }
73      }
74  
75      public static HttpRequestBody xml(String xml, String encoding) {
76          try {
77              return new HttpRequestBody(xml.getBytes(encoding), ContentType.XML, encoding);
78          } catch (UnsupportedEncodingException e) {
79              throw new IllegalArgumentException("illegal encoding " + encoding, e);
80          }
81      }
82  
83      public static HttpRequestBody custom(byte[] body, String contentType, String encoding) {
84          return new HttpRequestBody(body, contentType, encoding);
85      }
86  
87      public static HttpRequestBody form(Map<String,Object> params, String encoding){
88          List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(params.size());
89          for (Map.Entry<String, Object> entry : params.entrySet()) {
90              nameValuePairs.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
91          }
92          try {
93              return new HttpRequestBody(URLEncodedUtils.format(nameValuePairs, encoding).getBytes(encoding), ContentType.FORM, encoding);
94          } catch (UnsupportedEncodingException e) {
95              throw new IllegalArgumentException("illegal encoding " + encoding, e);
96          }
97      }
98  
99      public byte[] getBody() {
100         return body;
101     }
102 }