HTTP 使用POST发送请求 request接受不到数据

javaweb 使用POST请求另一个服务器,服务器request获取不到数据 全部为null图片说明

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币 嘿嘿

你这个引用的类有点多,建议打断点调试一下。
再试试换个url行不行

试一下:

 HttpPost hp = new HttpPost("url");
 hp.setHeader("Content-Type", "application/json");  
 hp.setHeader("Accept", "application/json"); 
 hp.setEntity(new StringEntity(channelContext,"UTF-8"));

把这几句加上,试一下行不行。

 代码好像站错行了 先将就着看下吧

request.getParameter()必须需要一个参数名,一般是表单的name的值,如果没有名字就是从body里读取

 public static String readData(HttpServletRequest request) {
        BufferedReader br = null;
        try {
            StringBuilder ret;
            br = request.getReader();

            String line = br.readLine();
            if (line != null) {
                ret = new StringBuilder();
                ret.append(line);
            } else {
                return "";
            }

            while ((line = br.readLine()) != null) {
                ret.append('\n').append(line);
            }

            return ret.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        finally {
            if (br != null) {
                try {br.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);}
            }
        }
    }

httpclient 发送请求传输参数有两种方法
1. 模拟表单提交,传递键值对参数,后端可以通过 request.getParameter() 来取值;
2. 发送数据流,题主使用的正式这种方式,后端要通过流来读取参数,request.getInputStream() ;