欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java模擬post請求發(fā)送json的例子

 更新時間:2017年08月17日 16:45:18   作者:veitch-w  
本篇文章主要介紹了java模擬post請求發(fā)送json的例子,具有一定的參考價值,感興趣的小伙伴們可以參考一下

java模擬post請求發(fā)送json,用兩種方式實現(xiàn),第一種是HttpURLConnection發(fā)送post請求,第二種是使用httpclient模擬post請求,

方法一:

package main.utils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtilTest {
  Log log = new Log(this.getClass());//初始化日志類
  /**
   * @作用 使用urlconnection
   * @param url
   * @param Params
   * @return
   * @throws IOException
   */
  public String sendPost(String url,String Params)throws IOException{
    OutputStreamWriter out = null;
    BufferedReader reader = null;
    String response="";
    try {
      URL httpUrl = null; //HTTP URL類 用這個類來創(chuàng)建連接
      //創(chuàng)建URL
      httpUrl = new URL(url);
      //建立連接
      HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
      conn.setRequestMethod("POST");
      conn.setRequestProperty("Content-Type", "application/json");
      conn.setRequestProperty("connection", "keep-alive");
      conn.setUseCaches(false);//設(shè)置不要緩存
      conn.setInstanceFollowRedirects(true);
      conn.setDoOutput(true);
      conn.setDoInput(true);
      conn.connect();
      //POST請求
      out = new OutputStreamWriter(
          conn.getOutputStream());
      out.write(Params);
      out.flush();
      //讀取響應(yīng)
      reader = new BufferedReader(new InputStreamReader(
          conn.getInputStream()));
      String lines;
      while ((lines = reader.readLine()) != null) {
        lines = new String(lines.getBytes(), "utf-8");
        response+=lines;
      }
      reader.close();
      // 斷開連接
      conn.disconnect();

      log.info(response.toString());
    } catch (Exception e) {
    System.out.println("發(fā)送 POST 請求出現(xiàn)異常!"+e);
    e.printStackTrace();
    }
    //使用finally塊來關(guān)閉輸出流、輸入流
    finally{
    try{
      if(out!=null){
        out.close();
      }
      if(reader!=null){
        reader.close();
      }
    }
    catch(IOException ex){
      ex.printStackTrace();
    }
  }

    return response;
  }
}

方法二:使用httpclient實現(xiàn)

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import main.utils.Log;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

//post請求方法
  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;
  }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringSecurity 手機號登錄功能實現(xiàn)

    SpringSecurity 手機號登錄功能實現(xiàn)

    這篇文章主要介紹了SpringSecurity 手機號登錄功能實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧
    2023-12-12
  • Java was started but returned exit code=13問題解決案例詳解

    Java was started but returned exit code=13問題解決案例詳解

    這篇文章主要介紹了Java was started but returned exit code=13問題解決案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • 關(guān)于JDK+Tomcat+eclipse+MyEclipse的配置方法,看這篇夠了

    關(guān)于JDK+Tomcat+eclipse+MyEclipse的配置方法,看這篇夠了

    關(guān)于JDK+Tomcat+eclipse+MyEclipse的配置問題,很多朋友都搞不太明白,網(wǎng)上一搜配置方法多種哪種最精簡呢,今天小編給大家分享一篇文章幫助大家快速掌握J(rèn)DK Tomcat eclipse MyEclipse配置技巧,需要的朋友參考下吧
    2021-06-06
  • Java集合ArrayDeque類實例分析

    Java集合ArrayDeque類實例分析

    這篇文章主要介紹了Java集合ArrayDeque類實例分析的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • spring boot設(shè)置過濾器、監(jiān)聽器及攔截器的方法

    spring boot設(shè)置過濾器、監(jiān)聽器及攔截器的方法

    這篇文章主要給大家介紹了關(guān)于spring boot設(shè)置過濾器、監(jiān)聽器及攔截器的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Javaweb resin4如何配置端口虛擬目錄

    Javaweb resin4如何配置端口虛擬目錄

    這篇文章主要介紹了Javaweb resin4如何配置端口虛擬目錄,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • SpringBoot實現(xiàn)緩存組件配置動態(tài)切換的步驟詳解

    SpringBoot實現(xiàn)緩存組件配置動態(tài)切換的步驟詳解

    現(xiàn)在有多個springboot項目,但是不同的項目中使用的緩存組件是不一樣的,有的項目使用redis,有的項目使用ctgcache,現(xiàn)在需要用同一套代碼通過配置開關(guān),在不同的項目中切換這兩種緩存,本文介紹了SpringBoot實現(xiàn)緩存組件配置動態(tài)切換的步驟,需要的朋友可以參考下
    2024-07-07
  • Java注解處理器學(xué)習(xí)之編譯時處理的注解詳析

    Java注解處理器學(xué)習(xí)之編譯時處理的注解詳析

    編譯時注解相信對每一個java開發(fā)者來說都不陌生,下面這篇文章主要給大家介紹了關(guān)于Java注解處理器學(xué)習(xí)之編譯時處理的注解的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧
    2018-05-05
  • SpringBoot集合Mybatis過程解析

    SpringBoot集合Mybatis過程解析

    這篇文章主要介紹了SpringBoot集合Mybatis過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • SpringBoot+Spring Security無法實現(xiàn)跨域的解決方案

    SpringBoot+Spring Security無法實現(xiàn)跨域的解決方案

    這篇文章主要介紹了SpringBoot+Spring Security無法實現(xiàn)跨域的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評論