javaweb 使用POST请求另一个服务器,服务器request获取不到数据 全部为null
6条回答
- a274433141 2018-07-09 14:12关注
request获取的参数时form-encoding形式的参数,你传标注json格式的数据过去是解析不出来的,你可以按我贴的代码这样包装一下试试看。
这是httpClient调用类
public static String httpPost(List params, String url) throws Exception {
String body = "";
HttpPost httpPost = null;
String requestParams = "";
try {
DefaultHttpclient httpClient = new DefaultHttpclient();
httpPost = new HttpPost();
// 设置参数
httpPost.setURI(new URI(url));
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
requestParams = EntityUtils.toString(new UrlEncodedFormEntity(params));
// 发送请求
HttpResponse httpresponse = httpClient.execute(httpPost);
// 获取返回数据
HttpEntity entity = httpresponse.getEntity();
body = EntityUtils.toString(entity, "UTF-8");if (entity != null) { EntityUtils.consume(entity); } } catch (ConnectException ce) { ce.printStackTrace(); logger.error("ConnectException " + url + " 连接异常," + requestParams); } catch (Exception e) { e.printStackTrace(); logger.error("Exception: " + url + " 异常," + requestParams); } finally { try { if (httpPost != null) { httpPost.releaseConnection(); } } catch (Exception e) { logger.error("Exception: release http connection error!"); } } return body; }
这是参数包装
List params = new ArrayList();
params.add(new BasicNameValuePair("storeid", “123”)));
params.add(new BasicNameValuePair("load",“2323”)));请试一下吧,若解决问题请搭赏几个c币 嘿嘿
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 maple软件,用solve求反函数出现rootof,怎么办?
- ¥50 汇编语言除法溢出问题
- ¥65 C++实现删除N个数据列表共有的元素
- ¥15 Visual Studio问题
- ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
- ¥20 求一个html代码,有偿
- ¥100 关于使用MATLAB中copularnd函数的问题
- ¥20 在虚拟机的pycharm上
- ¥15 jupyterthemes 设置完毕后没有效果
- ¥15 matlab图像高斯低通滤波