java如何實(shí)現(xiàn)postman中用x-www-form-urlencoded參數(shù)的請(qǐng)求
java postman用x-www-form-urlencoded參數(shù)的請(qǐng)求
首先,先給出postman的參數(shù)構(gòu)造:

java代碼實(shí)現(xiàn)(以post方法為例)
PostMethod postMethod = new PostMethod(valueConfig.getImpsAuthUrl()) ;
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
//參數(shù)設(shè)置,需要注意的就是里邊不能傳NULL,要傳空字符串
//key value 形式參數(shù)
NameValuePair[] data = {
new NameValuePair("username","test"),
new NameValuePair("password","test123")
};
postMethod.setRequestBody(data);
HttpClient httpClient = new HttpClient();
int response = httpClient.executeMethod(postMethod); // 執(zhí)行POST方法
String result = postMethod.getResponseBodyAsString() ; //返回結(jié)果
if (response == 200 && result != null) {
//成功后的邏輯
doSth();
log.info("獲取請(qǐng)求,result={}", result);
}java實(shí)現(xiàn)postman為x-www-form-urlencoded的調(diào)用
客戶(hù)端實(shí)現(xiàn)
導(dǎo)入http-client jar。
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>public static void clientDemo() {
try {
String requestUrl = "http://hello/demo";
PostMethod postMethod = new PostMethod(requestUrl);
String data = "json_json_json_json";
StringRequestEntity stringRequestEntity = new StringRequestEntity(data, "application/x-www-form-urlencoded", "utf-8");
postMethod.setRequestEntity(stringRequestEntity);
org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
//調(diào)用接口
int code = httpClient.executeMethod(postMethod);
String result = postMethod.getResponseBodyAsString();
System.out.println("接口狀態(tài)" + code);
System.out.println("響應(yīng)" + result);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}服務(wù)端實(shí)現(xiàn)
@RequestMapping(value = "/receive", method = RequestMethod.POST)
@ResponseBody
public String receiveFare(@RequestBody String str) {
System.out.println("接到數(shù)據(jù):" + str);
return "ok";
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用Apache POI和EasyExcel讀取Excel文件的實(shí)現(xiàn)方案
Java 讀取 Excel 文件核心依賴(lài) Apache POI(兼容 .xls(Excel 97-2003)和 .xlsx(Excel 2007+))或 EasyExcel(阿里開(kāi)源,低內(nèi)存、高性能),以下是兩種主流方案的完整實(shí)現(xiàn),需要的朋友可以參考下2025-12-12
MyBatis之set標(biāo)簽使用及說(shuō)明
MyBatis中update語(yǔ)句使用set標(biāo)簽可以動(dòng)態(tài)更新列,并且可以剔除多余的逗號(hào),if+set標(biāo)簽可以避免某項(xiàng)為null時(shí)進(jìn)行更新2026-01-01
Spring與MyBatis集成?AOP整合PageHelper插件的操作過(guò)程
Spring與MyBatis集成的主要目的是為了提供更強(qiáng)大的數(shù)據(jù)訪(fǎng)問(wèn)和事務(wù)管理能力,以及簡(jiǎn)化配置和提高開(kāi)發(fā)效率,這篇文章主要介紹了Spring與MyBatis集成AOP整合PageHelper插件,需要的朋友可以參考下2023-08-08
Java實(shí)現(xiàn)消消樂(lè)中的消除功能
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)消消樂(lè)中的消除功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
Java基于裝飾者模式實(shí)現(xiàn)的圖片工具類(lèi)實(shí)例【附demo源碼下載】
這篇文章主要介紹了Java基于裝飾者模式實(shí)現(xiàn)的圖片工具類(lèi),結(jié)合完整實(shí)例形式分析了裝飾者模式實(shí)現(xiàn)圖片的判斷、水印、縮放、復(fù)制等功能,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-09-09
MyBatis查詢(xún)無(wú)記錄時(shí)的返回值問(wèn)題
這篇文章主要介紹了MyBatis查詢(xún)無(wú)記錄時(shí)的返回值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

