zoukankan      html  css  js  c++  java
  • HttpUrlConneciton上传JSON数据

        try {
                //创建连接 
                URL url = new URL(url);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setRequestMethod("POST");
                connection.setUseCaches(false);
                connection.setInstanceFollowRedirects(true);
                connection.setRequestProperty("Content-Type", "application/json");
                connection.connect();
                // POST请求 
                DataOutputStream out = new DataOutputStream(connection.getOutputStream());
                JSONObject obj = new JSONObject();
                String json = java.net.URLEncoder.encode(obj.toString(), "utf-8");
                out.writeBytes(json);
                out.flush();
                out.close();
                // 读取响应 
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String lines;
                StringBuffer sb = new StringBuffer("");
                while ((lines = reader.readLine()) != null) {
                    lines = URLDecoder.decode(lines, "utf-8");
                    sb.append(lines);
                }
                System.out.println(sb);
                reader.close();
                // 断开连接 
                connection.disconnect(); 
            } catch (MalformedURLException e) { 
                e.printStackTrace(); 
            } catch (UnsupportedEncodingException e) { 
                e.printStackTrace(); 
            } catch (IOException e) { 
                e.printStackTrace(); 
            } 
    复制代码
    
  • 相关阅读:
    ATOM编辑器插件
    说说关于IE浏览器兼容性
    git命令
    Vue js 的生命周期详解
    Flexbox 布局教程
    到底vuex是什么?
    CSS3 动画 animation和@keyframes
    zabbix添加触发器Triggers
    zabbix邮件报警
    修改zabbix监控项刷新时间
  • 原文地址:https://www.cnblogs.com/hualuoshuijia/p/9951844.html
Copyright © 2011-2022 走看看