zoukankan      html  css  js  c++  java
  • java模拟post请求发送json

    java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求。

    方法一:

    public static String sendPost(String url_param, String param) {
            String result = "";// 返回的结果  
            BufferedReader in = null;// 读取响应输入流  
    //        PrintWriter out = null;  
            try {  
                // 创建URL对象  
                URL url = new URL(url_param);
                // 打开URL连接  
                java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) url  
                        .openConnection();  
                // 设置属性  
                httpConn.setRequestProperty("Content-Type",
                        "application/json"); 
                // 设置POST方式  
                httpConn.setDoInput(true);  
                httpConn.setDoOutput(true);  
                httpConn.setRequestMethod("POST");
                httpConn.setUseCaches(false);
                httpConn.setInstanceFollowRedirects(true);
                // 获取HttpURLConnection对象对应的输出流  
                DataOutputStream out = new DataOutputStream(httpConn.getOutputStream());
                out.write(param.getBytes("utf-8"));
                // flush输出流的缓冲  
                out.flush();  
                out.close(); 
                // 定义BufferedReader输入流来读取URL的响应,设置编码方式  
                in = new BufferedReader(new InputStreamReader(httpConn  
                        .getInputStream(), "UTF-8"));  
                String line;  
                // 读取返回的内容  
                while ((line = in.readLine()) != null) {  
                    result += line;  
                }  
            } catch (Exception e) {  
                e.printStackTrace();  
            } finally {  
                try {  
                  /*  if (out != null) {  
                        out.close();  
                    }  */
                    if (in != null) {  
                        in.close();  
                    }  
                } catch (IOException ex) {  
                    ex.printStackTrace();  
                }  
            }  
            return result;  
        }

    方法二:

     public  String sendPost(String url, String data) {
              String response = null;
              log.info("url: " + url);
             log.info("request: " + data);
              try {
                 CloseableHttpClient httpclient = null;
                   CloseableHttpResponse httpresponse = null;
                 try {
                      httpclient = HttpClients.createDefault();
                      HttpPost httppost = new HttpPost(url);
                      StringEntity stringentity = new StringEntity(data,
                              ContentType.create("text/json", "UTF-8"));
                     httppost.setEntity(stringentity);
                      httpresponse = httpclient.execute(httppost);
                     response = EntityUtils
                              .toString(httpresponse.getEntity());
                      log.info("response: " + response);
                  } finally {
                     if (httpclient != null) {
                          httpclient.close();
                     }
                     if (httpresponse != null) {
                         httpresponse.close();
                      }
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
             return response;
         }
    

      

  • 相关阅读:
    MySql 应用语句
    MySql 存储过程 退出
    MySql 存储过程 光标只循环一次
    MySql获取两个日期间的时间差
    VM VirtualBox 全屏模式 && 自动缩放模式 相互切换
    区分不同操作系统、编译器不同版本的宏
    debian下配置网络 安装无线网卡驱动 Broadcom BCMXX系列
    YII 主题设置
    Unix线程概念、控制原语、属性
    远程IPC种植木马
  • 原文地址:https://www.cnblogs.com/wqj-blog/p/6654705.html
Copyright © 2011-2022 走看看