HttpTools.java
4.96 KB
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
package com.xkl;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;
/**
* Created by win7 on 2016/12/25.
*/
public class HttpTools {
/**
* 获取当前时间,10位
* @return
*/
public static String getNow(){
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
return String.valueOf(c.getTimeInMillis()/1000);
}
/**
* 获取签名
*
* 加密算法: sign=md5(str + t + key),32位小写md5
* t: 北京时间距离1970年1月1日的秒数,10位
* key:平台对应的key,此值喜开路管理员授予
* str:业务参数数组,按参数名递增排序排列拼接,例如用户登陆接口,coid+password+username
*
* 需要验证:
* sign:是否正确
* t:是否过期,客户端与服务器时间必须为±300s以内,否则时间验证过期。
* @param params
* @return
*/
public static String getSign(Map<String, String> params){
String sign = "";
String paramStr = "";
List<String> keys = new ArrayList<String>();
for(Map.Entry<String, String> entry : params.entrySet()) {
keys.add(entry.getKey());
}
Collections.sort(keys);
for(String para:keys){
if(!para.equals("sign")&&!para.equals("t")&&!para.equals("type"))
paramStr += params.get(para);
}
sign = EncodeTools.encode("MD5",paramStr + params.get("t") + Constants.KEY);
return sign;
}
/**
* 发送http请求
* @param url 请求url
* @param type 请求类型:GET,POST,DELETE
* @param params 请求参数:map类型
* @return
*/
public static String requestByMapWithToken(String url, String type, Map<String, String> params, String authorization){
String reqbody = getRequestData(params,"UTF-8").toString();
String result = requestByString(url + "?" + reqbody,type,authorization,null);
return result;
}
public static String requestByMap(String url, String type,Map<String, String> params){
return requestByMapWithToken(url,type,params,"");
}
/**
* 发送http请求
* @param url 请求url
* @param type 请求类型:GET,POST,DELETE
* @param reqbody 请求参数:String类型
* @return
*/
public static String requestByString(String url, String type, String authorization,String reqbody){
HttpURLConnection con = null;
String result = null;
try {
con = getHttpConnection( url , type, authorization);
//you can add any request body here if you want to post
if( reqbody != null){
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(reqbody);
out.flush();
out.close();
}
con.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String temp = null;
StringBuilder sb = new StringBuilder();
while((temp = in.readLine()) != null){
sb.append(temp).append(" ");
}
result = sb.toString();
in.close();
} catch (IOException e) {
System.out.println(e);
}
return result;
}
private static HttpURLConnection getHttpConnection(String url, String type, String authorization){
URL uri = null;
HttpURLConnection con = null;
try{
uri = new URL(url);
con = (HttpURLConnection) uri.openConnection();
con.setRequestMethod(type); //type: POST, PUT, DELETE, GET
if(type.equals("POST"))
con.setDoOutput(true);
con.setConnectTimeout(60000); //60 secs
con.setReadTimeout(60000); //60 secs
con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
con.setRequestProperty("authorization",authorization);
}catch(Exception e){
System.out.println( "connection i/o failed" );
}
return con;
}
private static StringBuffer getRequestData(Map<String, String> params, String encode) {
StringBuffer stringBuffer = new StringBuffer(); //存储封装好的请求体信息
try {
for(Map.Entry<String, String> entry : params.entrySet()) {
stringBuffer.append(entry.getKey())
.append("=")
.append(URLEncoder.encode(entry.getValue(), encode))
.append("&");
}
stringBuffer.deleteCharAt(stringBuffer.length() - 1); //删除最后的一个"&"
} catch (Exception e) {
e.printStackTrace();
}
return stringBuffer;
}
}