记录http请求 body内容的添加和接收

本文介绍了一种使用Java实现的HTTP请求与响应处理方法,包括如何构造带有JSON体的POST请求,以及如何解析HTTP响应中的JSON数据。同时,还提供了不同版本的HttpClient设置超时时间的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//http请求 body内容添加

public static void do_json_req(){
        HttpPost request = new HttpPost("http://localhost:8080/DSQ_Payment/uccb.do");
        JSONObject param_data = new JSONObject();
        param_data.put("orderId", "abcf1330");
        param_data.put("gameId", 123);
        param_data.put("accountId", "12221222211123");
        param_data.put("creator", "JY");
        param_data.put("payWay", 1);
        param_data.put("amount", "100.00");
        param_data.put("callbackInfo", "custominfo=xxxxx#user=xxxx");
        param_data.put("orderStatus", "S");
        param_data.put("failedDesc", "");
        param_data.put("cpOrderId", "1234567");
        
        JSONObject param = new JSONObject();
        
        param.put("ver", "2.0");
        param.put("data", param_data);
        param.put("sign", "6362e564f832d2e8bbcbd50e75409d47");
        // 绑定到请求Entry
        StringEntity se;
        try {
            se = new StringEntity(param.toString());
            request.setEntity(se);
            // 发送请求
            HttpResponse httpResponse = new DefaultHttpClient().execute(request);
            String retSrc = EntityUtils.toString(httpResponse.getEntity());
            // 生成 JSON 对象
//            JSONObject result = new JSONObject( retSrc);
//            String token = result.get("token");
            System.out.println(retSrc);
        } catch (Exception e) {
            e.printStackTrace();
        }


//http body内容接收

public String ucCB(HttpServletRequest req){
        
        UcpayCBVO uccbvo=null;
        String ret = "error....";
        try {
            BufferedReader bf = new BufferedReader(new InputStreamReader(req.getInputStream()));
            String line = "";
            String reqstr = "";
            while((line=bf.readLine()) != null){
                reqstr += line;
            }
            
            if(null != reqstr && !"".equals(reqstr)){
                Map<String, Object> f_map  =PayUtil.jsonToMap(reqstr);
                Map<String, Object> map_json =(Map<String, Object>)f_map.get("data");
                map_json.put("ver", f_map.get("ver"));
                map_json.put("sign", f_map.get("sign"));
                reqstr=PayUtil.ObjectToJsonStr(map_json);
                uccbvo = (UcpayCBVO) PayUtil.jsonToObject(reqstr, UcpayCBVO.class);
                ret = ucService.callBackPayCenter(uccbvo);
            }
            log.debug("uccb: " + PayUtil.ObjectToJsonStr(uccbvo));
        } catch (Exception e) {
            log.error("uc cb error!", e);
        }
        log.debug("uc cb ret=" + ret + "; orderid=" + uccbvo.getOrderId());
        return ret;
    }


不是全部的代码,留下主要的两部分,给以后做参考~~

以下是HttpClient不同版本超时时间的设置

3.X是这样的

HttpClient client=new DefaultHttpClient();
client.setConnectionTimeout(30000); 
client.setTimeout(30000);
4.X是这样的
HttpClient httpClient=new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,2000);//连接时间
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,2000);//数据传输时间
4.3是这样的
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet=new HttpGet("http://www.baidu.com");//HTTP Get请求(POST雷同)
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();//设置请求和传输超时时间
httpGet.setConfig(requestConfig);
httpClient.execute(httpGet);//执行请求





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值