7-26 第五天

终于把postman的测试搞定了
其实就是使用java内置类:HttpServer, 然后构建一个简易的能接收post请求的http服务器, 然后接收参数, 执行函数.

HttpServerStarter类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.wakabaka;

import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

public class HttpServerStarter {
public static void main(String[] args) throws IOException {
//创建一个HttpServer实例,并绑定到指定的IP地址和端口号
HttpServer httpServer = HttpServer.create(new InetSocketAddress(8080), 0);

//创建一个HttpContext,将路径为/myserver请求映射到MyHttpHandler处理器
httpServer.createContext("/myserver", new MyHttpHandler());

//设置服务器的线程池对象
httpServer.setExecutor(Executors.newFixedThreadPool(10));

//启动服务器
httpServer.start();
}
}

myHttpHandler类:

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.wakabaka;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.ScanResult;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* 处理/myserver路径请求的处理器类
*/
public class MyHttpHandler implements HttpHandler {
@Override
public void handle(HttpExchange httpExchange) {
try {
StringBuilder responseText = new StringBuilder();
responseText.append("请求方法:").append(httpExchange.getRequestMethod()).append("<br/>");
responseText.append("请求参数:").append(getRequestParam(httpExchange)).append("<br/>");
responseText.append("请求头:<br/>").append(getRequestHeader(httpExchange));
handleResponse(httpExchange, responseText.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
}

/**
* 获取请求头
* @param httpExchange
* @return
*/
private String getRequestHeader(HttpExchange httpExchange) {
Headers headers = httpExchange.getRequestHeaders();
return headers.entrySet().stream()
.map((Map.Entry<String, List<String>> entry) -> entry.getKey() + ":" + entry.getValue().toString())
.collect(Collectors.joining("<br/>"));
}

/**
* 获取请求参数
* @param httpExchange
* @return
* @throws Exception
*/
private String getRequestParam(HttpExchange httpExchange) throws Exception {
String paramStr = "";

if (httpExchange.getRequestMethod().equals("GET")) {
//GET请求读queryString
paramStr = httpExchange.getRequestURI().getQuery();
} else {
//非GET请求读请求体
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpExchange.getRequestBody(), "utf-8"));
StringBuilder requestBodyContent = new StringBuilder();
String line = null;
String x = null;

while ((line = bufferedReader.readLine()) != null) {


x = decodeURL(line);
System.out.println(x);
requestBodyContent.append(x);

int index = x.indexOf("&");
//System.out.println(index);
//根据第一个点的位置 获得第二个点的位置
String result = x.substring(index);
index = result.indexOf("=");
String tmp = result.substring(index + 1);
System.out.println(tmp);
if(x.charAt(3) == 'n') {
jingquedel(tmp);
}
else {
mohudel(tmp);
}

}

paramStr = requestBodyContent.toString();
}

return paramStr;
}

/**
* 处理响应
* @param httpExchange
* @param responsetext
* @throws Exception
*/
private void handleResponse(HttpExchange httpExchange, String responsetext) throws Exception {
//生成html
StringBuilder responseContent = new StringBuilder();
responseContent.append("<html>")
.append("<body>")
.append(responsetext)
.append("</body>")
.append("</html>");
String responseContentStr = responseContent.toString();
byte[] responseContentByte = responseContentStr.getBytes("utf-8");

//设置响应头,必须在sendResponseHeaders方法之前设置!
httpExchange.getResponseHeaders().add("Content-Type:", "text/html;charset=utf-8");

//设置响应码和响应体长度,必须在getResponseBody方法之前调用!
httpExchange.sendResponseHeaders(200, responseContentByte.length);

OutputStream out = httpExchange.getResponseBody();
out.write(responseContentByte);
out.flush();
out.close();
}

public static String decodeURL(String url){
return url.replace("%2B", "+").replace("%20", " ")
.replace("%2F", "/").replace("%3F", "?")
.replace("%23", "#").replace("%26", "&")
.replace("%3D", "=").replace("%25", "%")
.replace("%3A", ":").replace("%2A", "*");
}


public static void jingquedel(String s) {
//Jedis jedis = new Jedis("192.168.43.116", 6379);
Jedis jedis = new Jedis("172.22.56.65", 6380);
jedis.auth("123456");

jedis.del(s);

jedis.close();
}

public static void mohudel(String s){

//Jedis jedis = new Jedis("192.168.43.116", 6379);
Jedis jedis = new Jedis("172.22.56.65", 6380);
jedis.auth("123456");

ScanParams scanParams = new ScanParams();
scanParams.match(s);
scanParams.count(5);

ScanResult<String> scan = jedis.scan("0", scanParams);
List<String> keys = scan.getResult();
int num = 0;
for(String key : keys) {
jedis.del(key);
//System.out.println(key);
num++;
}
jedis.close();
}
}