HttpServletRequest.getParameters()对于空格的处理

本文探讨了在使用HttpServletRequest获取URL参数时,如何处理其中的空格和+号问题。特别是通过实例说明了不同编码方式下参数的正确处理方法,强调了在发送GET请求时对参数进行适当编码的重要性。

今天才知道原来HttpServletRequest.getParameters()对于空格的处理是这样的!

url: xxx?p=a+b
request.getParameters("p"): a b(a与b中间是空格)

url: xxx?p=a b(a与b中间是空格)
request.getParameters("p"): a b(a与b中间是空格)

url: xxx?p=a%20b
request.getParameters("p"): a b(a与b中间是空格)

url: xxx?p=a%2Bb
request.getParameters("p"): a+b

直接用Get请求的时候,如果参数中有+号或者空格一定要看看是不是应该先encode一下。如果是空格用javascript的escape()就能encode,如果是+号就得自己转了。

curl -X POST "http://10.54.53.240:9996/imodbus/network" -H "Content-Type: application/json" -d '{ "deviceType": "SUN2000L", "deviceVer": "SUN2000L-5KTL", "modelType": "单相1.0", "softVer": "V100R001C00SPC334", "esn": "lxt_red_125569", "version": "8.0", "component": [ { "type": "Power Sensor", "prop": { "checked": true } }, { "type": "Energy Store", "prop": { "checked": true, "FE": true } }, { "type": "Optimizer", "prop": { "num": 2 } } ], "num": 1, "ip": "10.54.53.80", "port": "16101", "children": [], "connect": false }' -s -o /dev/null -w "Status: %{http_code}\n"这个是400,Completed 400 BAD_REQUEST 2025-11-06 16:49:12:206 [http-nio-9996-exec-4] DEBUG org.springframework.web.servlet.DispatcherServlet - "ERROR" dispatch for GET "/error", parameters={} 2025-11-06 16:49:12:206 [http-nio-9996-exec-4] DEBUG c.h.c.w.c.w.DefaultWushanRequestMappingHandlerMapping - Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest) 2025-11-06 16:49:12:207 [http-nio-9996-exec-4] DEBUG o.s.w.s.m.m.annotation.HttpEntityMethodProcessor - Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/yaml] 2025-11-06 16:49:12:207 [http-nio-9996-exec-4] DEBUG o.s.w.s.m.m.annotation.HttpEntityMethodProcessor - Writing [{timestamp=Thu Nov 06 16:49:12 CST 2025, status=400, error=Bad Request, path=/imodbus/network}] 2025-11-06 16:49:12:208 [http-nio-9996-exec-4] DEBUG org.springframework.web.servlet.DispatcherServlet - Exiting from "ERROR" dispatch, status 400
最新发布
11-07
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值