liukangdong
2024-05-21 4658e9353f7aa76a833d36ed988d77bf362ad0ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package cn.emay.sdk.util.http.request;
 
import java.util.Map;
 
/**
 * Http请求实体
 * 
 * @author Frank
 *
 * @param <T>
 *            传输数据类型
 */
public abstract class EmayHttpRequest<T extends Object> {
 
    private String url;// URL
    private String charSet = "UTF-8";// 编码
    private String method = "GET";// Http方法
    private Map<String, String> headers;// 头信息
    private String cookies;// cookie信息
    private T params;// 传输数据
 
    /**
     * 
     * @param url
     *            URL
     * @param charSet
     *            编码
     * @param method
     *            Http方法
     * @param headers
     *            头信息
     * @param cookies
     *            cookie信息
     * @param params
     *            传输数据
     */
    public EmayHttpRequest(String url, String charSet, String method, Map<String, String> headers, String cookies, T params) {
        if (url != null) {
            this.url = url.trim();
        }
        if (charSet != null) {
            this.charSet = charSet;
        }
        if (method != null) {
            this.method = method;
        }
        this.headers = headers;
        this.cookies = cookies;
        this.params = params;
        this.fillGetUrl();
    }
 
    /**
     * 将传输数据转换为byte[]类型
     * 
     * @return
     */
    public abstract byte[] paramsToBytesForPost();
 
    /**
     * 将传输数据转换为String类型
     * 
     * @return
     */
    public abstract String paramsToStringForGet();
 
    /**
     * 是否https请求
     * 
     * @return
     */
    public boolean isHttps() {
        if (url == null) {
            return false;
        }
        if (url.startsWith("https")) {
            return true;
        }
        return false;
    }
 
    /**
     * 构建GET URL
     */
    private void fillGetUrl() {
        if (url == null || params == null) {
            return;
        }
        if (this.getMethod().equalsIgnoreCase("GET")) {
            String getprams = this.paramsToStringForGet();
            if (url.indexOf("?") > 0) {
                url = url + "&" + getprams;
            } else {
                url = url + "?" + getprams;
            }
        }
    }
 
    public String getUrl() {
        return url;
    }
 
    public void setUrl(String url) {
        this.url = url;
    }
 
    public String getCharSet() {
        return charSet;
    }
 
    public void setCharSet(String charSet) {
        this.charSet = charSet;
    }
 
    public String getMethod() {
        return method;
    }
 
    public void setMethod(String method) {
        this.method = method;
    }
 
    public Map<String, String> getHeaders() {
        return headers;
    }
 
    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }
 
    public String getCookies() {
        return cookies;
    }
 
    public void setCookies(String cookies) {
        this.cookies = cookies;
    }
 
    public T getParams() {
        return params;
    }
 
    public void setParams(T params) {
        this.params = params;
    }
 
}