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),本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧2023-12-12Java 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的配置問題,很多朋友都搞不太明白,網(wǎng)上一搜配置方法多種哪種最精簡呢,今天小編給大家分享一篇文章幫助大家快速掌握J(rèn)DK Tomcat eclipse MyEclipse配置技巧,需要的朋友參考下吧2021-06-06spring 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-04SpringBoot實現(xiàn)緩存組件配置動態(tài)切換的步驟詳解
現(xiàn)在有多個springboot項目,但是不同的項目中使用的緩存組件是不一樣的,有的項目使用redis,有的項目使用ctgcache,現(xiàn)在需要用同一套代碼通過配置開關(guān),在不同的項目中切換這兩種緩存,本文介紹了SpringBoot實現(xiàn)緩存組件配置動態(tài)切換的步驟,需要的朋友可以參考下2024-07-07Java注解處理器學(xué)習(xí)之編譯時處理的注解詳析
編譯時注解相信對每一個java開發(fā)者來說都不陌生,下面這篇文章主要給大家介紹了關(guān)于Java注解處理器學(xué)習(xí)之編譯時處理的注解的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧2018-05-05SpringBoot+Spring Security無法實現(xiàn)跨域的解決方案
這篇文章主要介紹了SpringBoot+Spring Security無法實現(xiàn)跨域的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07