-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSigntureTest.java
More file actions
128 lines (107 loc) · 4.04 KB
/
Copy pathSigntureTest.java
File metadata and controls
128 lines (107 loc) · 4.04 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
package web.httpclient;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import net.sf.json.JSONObject;
public class SigntureTest {
public static void main(String[] args) throws Exception {
String mobile="18000";
// Map<String, Object> params =new HashMap<String,Object>();
// params.put("mobile", mobile);
sendPostData("http://localhost/wo_security_api/app/v1/flow/order?mobile="
+ mobile );
}
public static void sendPostData(String urlStr, Map<String, Object> params)
throws Exception {
StringBuilder sb = new StringBuilder();
String BOUNDARY = "----WebKitFormBoundaryT1HoybnYeFOGFlBR";
/**
* 封装请求参数
*/
for (String key : params.keySet()) {
sb.append("--" + BOUNDARY + "\r\n");
sb.append("Content-Disposition: form-data; name=\"" + key + "\""
+ "\r\n");
sb.append("\r\n");
sb.append(params.get(key) + "\r\n");
}
sb.replace(sb.lastIndexOf("\r\n"), sb.length(), "");
byte[] headerInfo = sb.toString().getBytes("UTF-8");
byte[] endInfo = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("UTF-8");
System.out.println(sb.toString());
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"form-data; boundary=" + BOUNDARY);
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
out.write(headerInfo);
out.write(endInfo);
if (conn.getResponseCode() == 200) {
System.out.println("成功!");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(conn
.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
public static void sendPostData(String urlStr) throws Exception {
StringBuilder sb = new StringBuilder();
String BOUNDARY = "----WebKitFormBoundaryT1HoybnYeFOGFlBR";
// sb.replace(sb.lastIndexOf("\r\n"), sb.length(), "");
byte[] headerInfo = sb.toString().getBytes("UTF-8");
byte[] endInfo = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("UTF-8");
System.out.println("test:"+sb.toString());
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"form-data; boundary=" + BOUNDARY);
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
out.write(headerInfo);
out.write(endInfo);
if (conn.getResponseCode() == 200) {
System.out.println("成功!");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(conn
.getInputStream()));
String line = reader.readLine();
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
public static String getServerTime(String urlStr) throws Exception {
StringBuilder sb = new StringBuilder();
String BOUNDARY = "----WebKitFormBoundaryT1HoybnYeFOGFlBR";
// sb.replace(sb.lastIndexOf("\r\n"), sb.length(), "");
byte[] headerInfo = sb.toString().getBytes("UTF-8");
byte[] endInfo = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("UTF-8");
System.out.println(sb.toString());
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + BOUNDARY);
conn.setDoOutput(true);
OutputStream out = conn.getOutputStream();
out.write(headerInfo);
out.write(endInfo);
if (conn.getResponseCode() == 200) {
System.out.println("成功!");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(conn
.getInputStream()));
String line = reader.readLine();
JSONObject json = JSONObject.fromObject(line);
JSONObject result = JSONObject.fromObject(json.get("result"));
String time = result.get("server_time").toString();
return time;
}
}