-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttpSender.java
More file actions
135 lines (123 loc) · 4.17 KB
/
HttpSender.java
File metadata and controls
135 lines (123 loc) · 4.17 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
/**
* Created by anyuan on 2017/6/4.
*/
public class HttpSender {
/**
* stringify params
*
* @param params url params
* @return string of params
*/
private static String urlParse(Map<String, String> params) {
StringBuilder stringify_url = new StringBuilder("?");
for (Map.Entry<String, String> entry :
params.entrySet()) {
stringify_url.append(entry.getKey());
stringify_url.append("=");
stringify_url.append(entry.getValue());
stringify_url.append("&");
}
stringify_url.deleteCharAt(stringify_url.length());
System.out.println(stringify_url);
return stringify_url.toString();
}
/**
* get data from a url
*
* @param url the url to request
* @param params url params
* @return res the response of server
*/
public static String sendGet(String url, Map<String, String> params, Map<String, String> Header) {
String res = "";
BufferedReader in = null;
try {
// generate url link
String encoded_url = url + urlParse(params);
URL realUrl = new URL(encoded_url);
// to get url connection
HttpURLConnection httpURLConnection = (HttpURLConnection) realUrl.openConnection();
// set some request headers
for (Map.Entry<String, String> entry :
Header.entrySet()) {
httpURLConnection.setRequestProperty(entry.getKey(),entry.getValue());
}
// connect(actually it doesn't send any data)
httpURLConnection.connect();
// define some streams
in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
res += line;
}
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
// close the stream
finally {
try {
assert in != null;
in.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return res;
}
/**
* post a data to a url
*
* @param url url to post
* @param params url params
* @return response of post result
*/
static String sendPost(String url, Map<String, String> params, byte[] data, Map<String, String> Header) {
OutputStream out = null;
BufferedReader in = null;
String res = "";
try {
String encoded_url = urlParse(params);
URL realUrl = new URL(url + encoded_url);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
for (Map.Entry<String, String> entry :
Header.entrySet()) {
conn.setRequestProperty(entry.getKey(),entry.getValue());
}
// must be set if post
conn.setDoOutput(true); //default false
conn.setDoInput(true); //default true
conn.setUseCaches(false); //mostly post doesn't use cache
conn.setRequestMethod("POST");
out = conn.getOutputStream();
//sending data to post(where different from get)
out.write(data);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
res += line;
}
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
} finally {
try {
assert out != null;
out.close();
assert in != null;
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return res;
}
}