|
@@ -16,46 +16,49 @@ public class HttpTools { |
|
@@ -16,46 +16,49 @@ public class HttpTools { |
16
|
|
16
|
|
17
|
/**
|
17
|
/**
|
18
|
* 获取当前时间,10位
|
18
|
* 获取当前时间,10位
|
|
|
19
|
+ *
|
19
|
* @return
|
20
|
* @return
|
20
|
*/
|
21
|
*/
|
21
|
- public static String getNow(){
|
22
|
+ public static String getNow() {
|
22
|
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
|
23
|
Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
|
23
|
- return String.valueOf(c.getTimeInMillis()/1000);
|
24
|
+ return String.valueOf(c.getTimeInMillis() / 1000);
|
24
|
}
|
25
|
}
|
25
|
|
26
|
|
26
|
|
27
|
|
27
|
/**
|
28
|
/**
|
28
|
* 发送http请求
|
29
|
* 发送http请求
|
29
|
- * @param url 请求url
|
|
|
30
|
- * @param type 请求类型:GET,POST,DELETE
|
30
|
+ *
|
|
|
31
|
+ * @param url 请求url
|
|
|
32
|
+ * @param type 请求类型:GET,POST,DELETE
|
31
|
* @param params 请求参数:map类型
|
33
|
* @param params 请求参数:map类型
|
32
|
* @return
|
34
|
* @return
|
33
|
*/
|
35
|
*/
|
34
|
- public static String requestByMapWithToken(String url, String type, Map<String, String> params, String authorization){
|
|
|
35
|
- String reqbody = getRequestData(params,"UTF-8").toString();
|
|
|
36
|
- String result = requestByString(url + "?" + reqbody,type,authorization,null);
|
36
|
+ public static String requestByMapWithToken(String url, String type, Map<String, String> params, String authorization) {
|
|
|
37
|
+ String reqbody = getRequestData(params, "UTF-8").toString();
|
|
|
38
|
+ String result = requestByString(url + "?" + reqbody, type, authorization, null);
|
37
|
return result;
|
39
|
return result;
|
38
|
}
|
40
|
}
|
39
|
|
41
|
|
40
|
- public static String requestByMap(String url, String type,Map<String, String> params){
|
|
|
41
|
- return requestByMapWithToken(url,type,params,"");
|
42
|
+ public static String requestByMap(String url, String type, Map<String, String> params) {
|
|
|
43
|
+ return requestByMapWithToken(url, type, params, "");
|
42
|
}
|
44
|
}
|
43
|
|
45
|
|
44
|
/**
|
46
|
/**
|
45
|
* 发送http请求
|
47
|
* 发送http请求
|
46
|
- * @param url 请求url
|
|
|
47
|
- * @param type 请求类型:GET,POST,DELETE
|
48
|
+ *
|
|
|
49
|
+ * @param url 请求url
|
|
|
50
|
+ * @param type 请求类型:GET,POST,DELETE
|
48
|
* @param reqbody 请求参数:String类型
|
51
|
* @param reqbody 请求参数:String类型
|
49
|
* @return
|
52
|
* @return
|
50
|
*/
|
53
|
*/
|
51
|
- public static String requestByString(String url, String type, String authorization,String reqbody){
|
54
|
+ public static String requestByString(String url, String type, String authorization, String reqbody) {
|
52
|
HttpURLConnection con = null;
|
55
|
HttpURLConnection con = null;
|
53
|
String result = null;
|
56
|
String result = null;
|
54
|
try {
|
57
|
try {
|
55
|
- con = getHttpConnection( url , type, authorization);
|
58
|
+ con = getHttpConnection(url, type, authorization);
|
56
|
//you can add any request body here if you want to post
|
59
|
//you can add any request body here if you want to post
|
57
|
- if( reqbody != null){
|
|
|
58
|
- DataOutputStream out = new DataOutputStream(con.getOutputStream());
|
60
|
+ if (reqbody != null) {
|
|
|
61
|
+ DataOutputStream out = new DataOutputStream(con.getOutputStream());
|
59
|
out.writeBytes(reqbody);
|
62
|
out.writeBytes(reqbody);
|
60
|
out.flush();
|
63
|
out.flush();
|
61
|
out.close();
|
64
|
out.close();
|
|
@@ -64,7 +67,7 @@ public class HttpTools { |
|
@@ -64,7 +67,7 @@ public class HttpTools { |
64
|
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
67
|
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
65
|
String temp = null;
|
68
|
String temp = null;
|
66
|
StringBuilder sb = new StringBuilder();
|
69
|
StringBuilder sb = new StringBuilder();
|
67
|
- while((temp = in.readLine()) != null){
|
70
|
+ while ((temp = in.readLine()) != null) {
|
68
|
sb.append(temp).append(" ");
|
71
|
sb.append(temp).append(" ");
|
69
|
}
|
72
|
}
|
70
|
result = sb.toString();
|
73
|
result = sb.toString();
|
|
@@ -76,21 +79,21 @@ public class HttpTools { |
|
@@ -76,21 +79,21 @@ public class HttpTools { |
76
|
|
79
|
|
77
|
}
|
80
|
}
|
78
|
|
81
|
|
79
|
- private static HttpURLConnection getHttpConnection(String url, String type, String authorization){
|
82
|
+ private static HttpURLConnection getHttpConnection(String url, String type, String authorization) {
|
80
|
URL uri = null;
|
83
|
URL uri = null;
|
81
|
HttpURLConnection con = null;
|
84
|
HttpURLConnection con = null;
|
82
|
- try{
|
85
|
+ try {
|
83
|
uri = new URL(url);
|
86
|
uri = new URL(url);
|
84
|
con = (HttpURLConnection) uri.openConnection();
|
87
|
con = (HttpURLConnection) uri.openConnection();
|
85
|
con.setRequestMethod(type); //type: POST, PUT, DELETE, GET
|
88
|
con.setRequestMethod(type); //type: POST, PUT, DELETE, GET
|
86
|
- if(type.equals("POST"))
|
89
|
+ if (type.equals("POST"))
|
87
|
con.setDoOutput(true);
|
90
|
con.setDoOutput(true);
|
88
|
- con.setConnectTimeout(60000); //60 secs
|
|
|
89
|
- con.setReadTimeout(60000); //60 secs
|
91
|
+ con.setConnectTimeout(2000); //2 secs
|
|
|
92
|
+ con.setReadTimeout(2000); //2 secs
|
90
|
con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
93
|
con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
91
|
- con.setRequestProperty("authorization",authorization);
|
|
|
92
|
- }catch(Exception e){
|
|
|
93
|
- System.out.println( "connection i/o failed" );
|
94
|
+ con.setRequestProperty("authorization", authorization);
|
|
|
95
|
+ } catch (Exception e) {
|
|
|
96
|
+ System.out.println("connection i/o failed");
|
94
|
}
|
97
|
}
|
95
|
|
98
|
|
96
|
|
99
|
|
|
@@ -100,7 +103,7 @@ public class HttpTools { |
|
@@ -100,7 +103,7 @@ public class HttpTools { |
100
|
private static StringBuffer getRequestData(Map<String, String> params, String encode) {
|
103
|
private static StringBuffer getRequestData(Map<String, String> params, String encode) {
|
101
|
StringBuffer stringBuffer = new StringBuffer(); //存储封装好的请求体信息
|
104
|
StringBuffer stringBuffer = new StringBuffer(); //存储封装好的请求体信息
|
102
|
try {
|
105
|
try {
|
103
|
- for(Map.Entry<String, String> entry : params.entrySet()) {
|
106
|
+ for (Map.Entry<String, String> entry : params.entrySet()) {
|
104
|
stringBuffer.append(entry.getKey())
|
107
|
stringBuffer.append(entry.getKey())
|
105
|
.append("=")
|
108
|
.append("=")
|
106
|
.append(URLEncoder.encode(entry.getValue(), encode))
|
109
|
.append(URLEncoder.encode(entry.getValue(), encode))
|