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

来源:互联网 发布:淘宝散片cpu去哪家买 编辑:程序博客网 时间:2024/06/08 11:25

//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;
    }


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






0 0
原创粉丝点击