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无用