1.客戶端實(shí)現(xiàn)
導(dǎo)入http-client jar。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-672424.html
<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());
}
}
2.服務(wù)端實(shí)現(xiàn)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-672424.html
@RequestMapping(value = "/receive", method = RequestMethod.POST)
@ResponseBody
public String receiveFare(@RequestBody String str) {
System.out.println("接到數(shù)據(jù):" + str);
return "ok";
}
到了這里,關(guān)于java實(shí)現(xiàn)postman為x-www-form-urlencoded的調(diào)用的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!