Java 方式进行Post提交

        public static String post(String ss) throws Exception {
                String code = "";
                
                //拼装参数
                Map postmap =  new HashMap();
                postmap.put("_imei",ss);

                
                //KeyService keyService = new KeyServiceImpl();
                //List<DataShopJson>  list = keyService.getProvincesByImei(key);
                
                JSONObject json = new JSONObject();
                json.accumulate("_authority_json", list);
                String str = json.toString();
                //去掉list的头部 -- no use
                //str = str.replace("\"list\":", "");
                postmap.put("_authority_json",str);
                
                logOper("单条激活");
                
                
                URL url=new URL(posturl);
                URLConnection rulConnection = url.openConnection();
                HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection; 
                httpUrlConnection.setDoOutput(true);   
                httpUrlConnection.setRequestMethod("POST");   
                httpUrlConnection.connect();  
                OutputStream outStrm = httpUrlConnection.getOutputStream();
                OutputStreamWriter objOutputStrm = new OutputStreamWriter(outStrm, "UTF-8");
                StringBuffer params = new StringBuffer();   

                Iterator it = postmap.entrySet().iterator();   
                while(it.hasNext()){   
                     Entry element = (Entry)it.next();   
                     params.append(element.getKey());   
                     params.append("=");   
                     params.append(element.getValue());   
                     params.append("&");   
                 }   
               
                if(params.length() > 0){   
                     params.deleteCharAt(params.length()-1);   
                }   
                objOutputStrm.write(params.toString());   
                objOutputStrm.flush();   
                objOutputStrm.close();   
                InputStream inStrm = httpUrlConnection.getInputStream();
                
                //获取接口的返回值
                StringBuffer info = new StringBuffer();
                BufferedInputStream buf = new BufferedInputStream(inStrm);
                byte[] buffer = new byte[1024];
                int iRead;
                while ((iRead = buf.read(buffer)) != -1) {
                    info.append(new String(buffer, 0, iRead, "UTF-8"));
                }                

               code = JSONObject.fromObject(info.toString()).get("_code").toString();
               DataShopPostUtil.logOper("处理结果code===="+code); 
               inStrm.close();
               
               return code.trim();
     }   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值