基于HTTP的HttpURLConnection POST请求

注意:在此期间由于中文乱码问题所以下文使用的是writeChars简要说明wrietCharswriteBytes的区别:

相同点writeBytes(String) writeChars(String) 都是接收 String 参数

不同点writeBytes(String) 依次写入字符串中的每一个字符,并且只写入字符的低8位,高字节被抛弃。

writeChars(String) 依次写入字符串中的每一个字符,字符的2个字节全部写入。 

使用POST请求传递JSON格式数据,示例如下:

   static JSONObject jsonResult = new JSONObject();

   static JSONObject jsonBody = new JSONObject();

   static JSONObject jsonData = new JSONObject();

public static void HeaderJson(String operatetype){

   JSONObject jsonHeader = new JSONObject(); 

   jsonHeader.put("orisys", "0"); 

   jsonHeader.put("operatetype", operatetype); 

   jsonBody.put("header", jsonHeader);

}

   public static String getResult(int size){

      if(size==0){

         jsonResult.put("status", "0"); 

         jsonResult.put("message", "推送成功");

         Object object = new Object();

         jsonResult.put("data", JSONObject.fromObject(object));

         System.out.println(jsonResult.toString());

         return jsonData.toString();

      }else{

         jsonResult.put("status", "1"); 

         jsonResult.put("message", "推送失败");

         Object object = new Object();

         jsonResult.put("data", JSONObject.fromObject(object));

         System.out.println(jsonResult.toString());

         return jsonResult.toString();

      }

     

   }

/**

    * 销售订单上报

    * @return

    * @throws SQLException

    */

   public static String salesOrder() throws SQLException {

       String result = "";

        try {

            URL url = new URL(SendUrl.SEND_URL_SalesOrder);

            HttpURLConnection connection = (HttpURLConnection) url

                    .openConnection();

           

            connection.setDoOutput(true);

            connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");

            connection.setRequestProperty("Charset", "UTF-8");

            connection.setDoInput(true);

            connection.setRequestMethod("POST");

            connection.setUseCaches(false);

            connection.setInstanceFollowRedirects(true);

            connection.setConnectTimeout(20000); 

            connection.setReadTimeout(300000); 

            connection.connect();

           

           

          

            DataOutputStream out = new DataOutputStream(

                    connection.getOutputStream());

           

           

            SelectPostDaoImpl send = new SelectPostDaoImpl();

          SalesOrder salesOrdersend.getOneSalesOrder();

     

          HeaderJson("add");

          jsonData.put("data", JSONObject.fromObject(salesOrder)); 

         

         

          jsonBody.put("body", jsonData.toString());

   

           // out.writeBytes(jsonBody.toString());

           out.writeChars(jsonBody.toString());

           

           

            out.flush();

            out.close();

           

           

           

            BufferedReader reader = new BufferedReader(new InputStreamReader(

                    connection.getInputStream()));

            String lines;

            StringBuffer sb = new StringBuffer("");

            while ((lines = reader.readLine()) != null) {

                lines = new String(lines.getBytes(), "utf-8");

                sb.append(lines);

            }

            System.out.println(sb);

            reader.close();

            connection.disconnect();

            result = getResult(0);

           // deleteService.delSalesOrder();//删除临时表单基本误差测试数据

          pressure_pro("salesOrderMidd");//调用存储过程,给临时表存放数据

        } catch (MalformedURLException e) {

          result = getResult(1);

            e.printStackTrace();

        } catch (UnsupportedEncodingException e) {

          result = getResult(1);

            e.printStackTrace();

        } catch (IOException e) {

          result = getResult(1);

            e.printStackTrace();

        }

      return result;

 

    }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值