常见问题
常见问题
请求或响应问题
1,参数值乱码或缺失
//以HttpClient为例
//a: 一般Get请求默认为此类型
import org.apache.http.client.methods.HttpGet;
httpGet.setHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
//或者b: post方式设置body格式为json
import org.apache.http.client.methods.HttpPost;
httpPost.setHeader("Content-Type","application/json;charset=UTF-8");
//c: 同时设置可能会报错,本文档也并无Post方式接口同时传递query参数和body参数//以HttpClient为例
// 传入的query参数
Map<String,String> queryParamMap ...;
//开始设置http请求参数
List<NameValuePair> param = new ArrayList<>();
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
param.add(new BasicNameValuePair(entry.getKey(),entry.getValue()) );
}
httpPost.setEntity(new UrlEncodedFormEntity(param, StandardCharsets.UTF_8));
//当传入的是body参数时
httpPost.setEntity(
new StringEntity(json,
ContentType.create("application/json", StandardCharsets.UTF_8)));
2,签名问题
异常提示
Last updated