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;
     }

  


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com