httpclient模擬post請求json封裝表單數(shù)據(jù)的實(shí)現(xiàn)方法
廢話不說上代碼:
public static String httpPostWithJSON(String url) throws Exception {
HttpPost httpPost = new HttpPost(url);
CloseableHttpClient client = HttpClients.createDefault();
String respContent = null;
// json方式
JSONObject jsonParam = new JSONObject();
jsonParam.put("name", "admin");
jsonParam.put("pass", "123456");
StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解決中文亂碼問題
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
System.out.println();
// 表單方式
// List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
// pairList.add(new BasicNameValuePair("name", "admin"));
// pairList.add(new BasicNameValuePair("pass", "123456"));
// httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));
HttpResponse resp = client.execute(httpPost);
if(resp.getStatusLine().getStatusCode() == 200) {
HttpEntity he = resp.getEntity();
respContent = EntityUtils.toString(he,"UTF-8");
}
return respContent;
}
public static void main(String[] args) throws Exception {
String result = httpPostWithJSON("http://localhost:8080/hcTest2/Hc");
System.out.println(result);
}
post方式 就要考慮提交的表單內(nèi)容怎么傳輸了。本文name和pass就是表單的值了。
封裝表單屬性可以用json也可以用傳統(tǒng)的表單,如果是傳統(tǒng)表單的話 要注意,也就是在上邊代碼注釋那部分。用這種方式的話在servlet里也就是數(shù)據(jù)處理層可以通過request.getParameter(”string“)直接獲取到屬性值。就是相比json這種要簡單一點(diǎn),不過在實(shí)際開發(fā)中一般都是用json做數(shù)據(jù)傳輸?shù)摹S胘son的話有兩種選擇一個(gè)是阿里巴巴的fastjson還有一個(gè)就是谷歌的gson。fastjson相比效率比較高,gson適合解析有規(guī)律的json數(shù)據(jù)。博主這里用的是fastjson。還有用json的話在數(shù)據(jù)處理層要用流來讀取表單屬性,這就是相比傳統(tǒng)表單多的一點(diǎn)內(nèi)容。代碼下邊已經(jīng)有了。
public class HcServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String acceptjson = "";
User user = new User();
BufferedReader br = new BufferedReader(new InputStreamReader(
(ServletInputStream) request.getInputStream(), "utf-8"));
StringBuffer sb = new StringBuffer("");
String temp;
while ((temp = br.readLine()) != null) {
sb.append(temp);
}
br.close();
acceptjson = sb.toString();
if (acceptjson != "") {
JSONObject jo = JSONObject.parseObject(acceptjson);
user.setUsername(jo.getString("name"));
user.setPassword(jo.getString("pass"));
}
request.setAttribute("user", user);
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
}
代碼比較簡陋,只是用于測試。希望能夠有所收獲。
以上就是小編為大家?guī)淼膆ttpclient模擬post請求json封裝表單數(shù)據(jù)的實(shí)現(xiàn)方法全部內(nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
一步步教你如何使用Java實(shí)現(xiàn)WebSocket
websocket協(xié)議是基于TCP的一種新的網(wǎng)絡(luò)協(xié)議,它實(shí)現(xiàn)了瀏覽器與服務(wù)器的全雙工通訊-允許服務(wù)器主動(dòng)發(fā)起信息個(gè)客戶端,websocket是一種持久協(xié)議,http是非持久協(xié)議,下面這篇文章主要給大家介紹了關(guān)于如何使用Java實(shí)現(xiàn)WebSocket的相關(guān)資料,需要的朋友可以參考下2023-05-05
深入理解MyBatis中的一級(jí)緩存與二級(jí)緩存
這篇文章主要給大家深入的介紹了關(guān)于MyBatis中一級(jí)緩存與二級(jí)緩存的相關(guān)資料,文中詳細(xì)介紹MyBatis中一級(jí)緩存與二級(jí)緩存的工作原理及使用,對大家具有一定的參考性學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06
java Gui實(shí)現(xiàn)肯德基點(diǎn)餐收銀系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java Gui實(shí)現(xiàn)肯德基點(diǎn)餐收銀系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
spring boot配合前端實(shí)現(xiàn)跨域請求訪問
本篇文章主要介紹了spring boot配合前端實(shí)現(xiàn)跨域請求訪問,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
springboot2.5.6集成RabbitMq實(shí)現(xiàn)Topic主題模式(推薦)
這篇文章主要介紹了springboot2.5.6集成RabbitMq實(shí)現(xiàn)Topic主題模式(推薦),pom.xml引入依賴和常量類創(chuàng)建,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-11-11
Springboot使用redis實(shí)現(xiàn)接口Api限流的示例代碼
本文主要介紹了Springboot使用redis實(shí)現(xiàn)接口Api限流的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Java項(xiàng)目開發(fā)命名規(guī)范(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)
定義這個(gè)規(guī)范的目的是讓項(xiàng)目中所有的文檔都看起來像一個(gè)人寫的,增加可讀性,減少項(xiàng)目組中因?yàn)閾Q人而帶來的損失。下面給大家分享java開發(fā)命名規(guī)范,一起看看吧2017-03-03

