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

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

6个回答

试一下:

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

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

qq_36311302
qq_36311302 HttpPost hp = new HttpPost("url"); hp.setHeader("Content-Type", "application/json"); hp.setHeader("Accept", "application/json"); hp.setEntity(new StringEntity(channelContext,"UTF-8"));
8 个月之前 回复
qq_40573659
锲而不舍的蜗牛 一般是路由和示图中函数不匹配,或是视图函数中引用模板的错误
一年多之前 回复
qq_30286285
qq1104682401 不行,是不是要用其他方式接收数据?request获取不到
一年多之前 回复
 代码好像站错行了 先将就着看下吧

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

qq_30286285
qq1104682401 就是服务器获取不到storeid和load的参数
一年多之前 回复

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

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() ;

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问