欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

状态码: 400 / Bad Request解决方案

最编程 2024-01-16 10:32:31
...

 

今天做项目的时候,需要向服务器接口 传递 json参数

 

请求 URL: http://localhost:8080/zjmarket55/gridturnpage?action=refresh&pk=-1&condition=aCondition%3D{"REQUEST_NAME":"1"}%26cond1%3D%257b%2522REQUEST_NAME%2522:%25221%2522%257d&tmpPercentWidth=1666&localcache=table&url_source=XMLHTTP

请求方法: POST

状态码: 400 / Bad Request

原因:

HttpGet或 HttpPost都不能传包含 ” 、“{“、”}”这样的参数,需要对特殊字符进行转义,把 ” 转成%22,把 { 转成%7b,把 } 转成%7d

 


JSONObject paramJson = new JSONObject();
paramJson.put("serviceType", "plat");
JSONObject queryData= new JSONObject();
queryData.put("name", google+"");
queryData.put("url", www.google.com+"");
paramJson.put("queryData", queryData);



Gson gson = new Gson();
 String gsonStr = paramJson.toString();
        //特殊字符进行转义 
        gsonStr = gsonStr.replace("\"", "%22")
                .replace("{", "%7b").replace("}", "%7d");