java模擬post請求發送json

java模擬post請求發送json,用兩種方式實現,第一種是HttpURLConnection發送post請求,第二種是使用httpclient模擬post請求。java

方法一:json

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

方法二:app

 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;
     }
相關文章
相關標籤/搜索