【发布时间】:2021-04-18 17:08:35
【问题描述】:
收到标准 JDK lib 的 html 表单帖子,并使用 HttpExchange 获取并传递给
private String getBodyString(HttpExchange he){
String ret=null;
InputStream is = he.getRequestBody();
URLDecoder dc = new URLDecoder();
try {
ret = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)).readLine();
ret = dc.decode(ret, StandardCharsets.UTF_8.name());
}catch (NullPointerException ex){
Logger.getLogger(RequestBodyParser.class.getName()).log(Level.SEVERE, null, ex);
}catch (IOException ex) {
Logger.getLogger(RequestBodyParser.class.getName()).log(Level.SEVERE, null, ex);
}
return ret;
}
返回:
mytxt1=huhu&mytxt2=haha&btnOk=1
其中 mytxt1 和 mytxt2 是输入。现在我想从中解析键(名称)和值(文本)。
这是一个很好的例子。我可以很容易地用“&”做一些字符串拆分,用“=”划分键和值。但也有不好的:
mytxt1=huhu & haha &mytxt2=haha&btnOk=1
...还有丑陋的:
mytxt1=Lorem &mytxt2=Ugly Injection&mytxt2=haha&btnOk=1
我还没有开始谈论“选择多个”。是否有任何最佳实践可以使用包含的 JDK 库以安全的方式解析表单条目?
【问题讨论】: