httpclient模擬post請(qǐng)求json封裝表單數(shù)據(jù)的實(shí)現(xiàn)方法
廢話不說(shuō)上代碼:
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");//解決中文亂碼問(wèn)題
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ù)處理層可以通過(guò)request.getParameter(”string“)直接獲取到屬性值。就是相比json這種要簡(jiǎn)單一點(diǎn),不過(guò)在實(shí)際開(kāi)發(fā)中一般都是用json做數(shù)據(jù)傳輸?shù)摹S胘son的話有兩種選擇一個(gè)是阿里巴巴的fastjson還有一個(gè)就是谷歌的gson。fastjson相比效率比較高,gson適合解析有規(guī)律的json數(shù)據(jù)。博主這里用的是fastjson。還有用json的話在數(shù)據(jù)處理層要用流來(lái)讀取表單屬性,這就是相比傳統(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);
}
}
代碼比較簡(jiǎn)陋,只是用于測(cè)試。希望能夠有所收獲。
以上就是小編為大家?guī)?lái)的httpclient模擬post請(qǐng)求json封裝表單數(shù)據(jù)的實(shí)現(xiàn)方法全部?jī)?nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
spring mvc 和ajax異步交互完整實(shí)例代碼
本篇文章主要介紹了spring mvc 和ajax異步交互完整實(shí)例代碼,簡(jiǎn)單的AJAX+SpringMVC的異步交互小例子,有興趣的可以了解一下。2017-02-02
解決若依pageHelper在動(dòng)態(tài)切換數(shù)據(jù)源問(wèn)題
這篇文章主要介紹了解決pageHelper在動(dòng)態(tài)切換數(shù)據(jù)源問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
springboot+mybatis如何屏蔽掉mybatis日志
這篇文章主要介紹了springboot+mybatis如何屏蔽掉mybatis日志問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
阿里Druid數(shù)據(jù)連接池引發(fā)的線上異常解決
這篇文章主要為大家介紹了一次關(guān)于阿里Druid數(shù)據(jù)連接池引發(fā)的線上異常問(wèn)題的解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
Mybatis查找返回Map,List集合類型的數(shù)據(jù)方式
這篇文章主要介紹了Mybatis查找返回Map,List集合類型的數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringMVC實(shí)現(xiàn)文件上傳下載的全過(guò)程
對(duì)于上傳功能,我們?cè)陧?xiàng)目中是經(jīng)常會(huì)用到的,比如用戶注冊(cè)的時(shí)候,上傳用戶頭像,這個(gè)時(shí)候就會(huì)使用到上傳的功能,而對(duì)于下載使用場(chǎng)景也很常見(jiàn),下面這篇文章主要給大家介紹了關(guān)于SpringMVC實(shí)現(xiàn)文件上傳下載的相關(guān)資料,需要的朋友可以參考下2022-01-01
Java中 ? extends T 和 ? super&nb
本文主要介紹了Java中 ? extends T 和 ? super T的理解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05

