Http的GetPost请求带有特殊字符,400 Bad Request解决方案

xiaoxiao2021-02-28  130

今天做项目的时候,需要向服务器接口 传递 json参数,使用 apache 的 httpclient,当时没有对参数做特殊处理直接提交了,结果运行时报错了,如下:

HTTP 400 Bad Request

后来在网上查资料找到原因了:HttpGet或 HttpPost都不能传包含 ” 、“{“、”}”这样的参数,需要对特殊字符进行转义,把 ” 转成",把 { 转成{,把 } 转成}

MsgResult msgResult = new MsgResult(1,"成功","我是正文内容"); Gson gson = new Gson(); String gsonStr = gson.toJson(msgResult); //特殊字符进行转义 gsonStr = gsonStr.replace("\"", """) .replace("{", "{").replace("}", "}"); Observable observable = webService_tomcat.insertJsonString(MethodName_MainHome,gsonStr); observable.subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer);

注意:参数里面如果有 空格的话,也需要转义,否则会有问题。

转载请注明原文地址: https://www.6miu.com/read-24194.html

最新回复(0)