• HttpURLConnection传json


    private static String sendToWangTing(DataRow dataRow) throws IOException{
            String ip = Configuration.getString("JZWangTing.ip");
            String port = Configuration.getString("JZWangTing.port"); 
            String service_id = Configuration.getString("JZWangTing.service_id"); 
            String url = Configuration.getString("JZWangTing.url"); 
            
            Map REQUESTS=new HashMap<String,String>();
            List list=new ArrayList();
            Map REQ_MSG_HDR=new HashMap<String,String>();
            REQ_MSG_HDR.put("OP_CODE", "8888");              //服务号
            REQ_MSG_HDR.put("OP_WAY", "1");                  //服务号
            REQ_MSG_HDR.put("MSG_ID", service_id);          //服务号
            REQ_MSG_HDR.put("OP_LANGUAGE", "1");          //服务号
            REQ_MSG_HDR.put("OP_CHANNEL", dataRow.get("channel_no"));              //渠道号
            REQ_MSG_HDR.put("SESSION_ID", "");              //sessionID
            
            
            Map REQ_COMM_DATA=new HashMap<String,String>();
            
            REQ_COMM_DATA.put("USER_CODE", dataRow.get("user_uuid"));            //用户唯一码
            REQ_COMM_DATA.put("service", service_id);                            //服务号
            REQ_COMM_DATA.put("CUST_CODE", dataRow.get("client_id"));            //客户号
            REQ_COMM_DATA.put("ACCESS_CHANNEL", dataRow.get("channel_no"));        //渠道编号
            REQ_COMM_DATA.put("ORGID", dataRow.get("branch_no"));                //营业部编号
            
            Map map1=new HashMap<String, Object>();
            map1.put("REQ_MSG_HDR", REQ_MSG_HDR);
            map1.put("REQ_COMM_DATA", REQ_COMM_DATA);
            //list.add(map2);
            list.add(map1);
            
            REQUESTS.put("REQUESTS",list);
            
            String date =JSONObject.fromObject(REQUESTS).toString();//转化成json
            System.out.println(date);
            
            String returninfo = "";
            
            URL httpUrl = new URL("http://"+ip+":"+port+url); 
            HttpURLConnection huc = (HttpURLConnection) httpUrl.openConnection(); 
            huc.setRequestMethod("POST");
            huc.setDoInput(true);  
            huc.setDoOutput(true);
            huc.setRequestProperty("Content-Type",  "application/json");  
            //链接地址  
            huc.connect(); 
            OutputStreamWriter writer = new OutputStreamWriter(huc.getOutputStream());  
            //发送参数  
            writer.write(date); 
          //清理当前编辑器的左右缓冲区,并使缓冲区数据写入基础流  
            writer.flush(); 
            BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream(),"UTF-8")) ;
            String line;
            Map js;
            while ((line = br.readLine()) != null) {
                returninfo = line;
                js=(Map)JSONObject.fromObject(returninfo); 
                System.out.println(js.toString());
            }        
            huc.connect();   
            br.close();
            return returninfo;
        }
        

    发送数据:

    {"REQUESTS":[{"REQ_COMM_DATA":{"USER_CODE":"EF48C567B74E4F8D952628EEDAF2C765","service":"B110004","CUST_CODE":"10100564286","ACCESS_CHANNEL":"1","ORGID":"0101"},"REQ_MSG_HDR":{"OP_WAY":"1","OP_CODE":"8888","MSG_ID":"B110004","OP_LANGUAGE":"1","SESSION_ID":"","OP_CHANNEL":"3"}}]}

    返回数据
    {"ANSWERS":[{"ANS_MSG_HDR":{"MSG_CODE":"999","MSG_TEXT":"","MSG_LEVEL":"","RESULT_NUM":"2","MSG_ID":"","PACKAGE_FLAG":"0","DATA_ROWS":"0","BPM_ROWS":"0","RUN_TIMES":""null"","MSG_TRACE":""},"ANS_COMM_DATA":[{"BPM_DATA":[]},{"CONFIG":{}}]}]}

  • 相关阅读:
    POJ Countries in War 3114
    POJ 2553 The Bottom of a Graph
    POJ 2762 Going from u to v or from v to u?(强联通 + TopSort)
    POJ 3180 The Cow Prom(强联通)
    HDU 4738 Caocao's Bridges
    喵哈哈村的几何大师╰☆莣メ誋こ月
    Vladik and Entertaining Flags
    XOR Queries
    Palindrome
    F
  • 原文地址:https://www.cnblogs.com/wangzhuxing/p/5834094.html
Copyright © 2020-2023  润新知