一、Java后台POST请求以application/x-www-form-urlencoded;
1、引入依赖:
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
2、写入代码:
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
private String post(String postURL,String name,String age) throws Exception{
PostMethod postMethod = new PostMethod(postURL) ;
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
//参数设置,需要注意的就是里边不能传NULL,要传空字符串
NameValuePair[] data = {
new NameValuePair("name",name),
new NameValuePair("age",age)
};
postMethod.setRequestBody(data);
org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
httpClient.executeMethod(postMethod);
String result = postMethod.getResponseBodyAsString() ;
return result;
}
二、Java后台POST请求raw的JSON的请求方式
1、写入代码:
//发送请求
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type","application/json");
Map<String,Object> map = new HashMap<>();
map.put("logitcsInterface",encryptStr);
map.put("apiCode","030001");
map.put("senderNo","1030005864872");
map.put("timeStamp",DateUtil.dataToStr(new Date()));
map.put("authorization","UZCCFaqJZTHV151C50");
HttpEntity<String> formEntity = new HttpEntity<>(map.toString(), headers);
RestTemplate restTemplate = new RestTemplate();
String urlAdress = "https://api.ems.com.cn/amp-prod-api/f/amp/api/test";//请求地址
ResponseEntity<String> responseEntity = restTemplate.postForEntity(urlAdress, formEntity, String.class);
String jsonData = responseEntity.getBody();
System.out.println(jsonData);
三、java实现postman中form-data传参方式
然后上次在用java实现一个向后台传参的形式时,在postman里以form-data的形式传参,我就尝试利用map进行数据的的封装Map<String,String>,结果发现后台无法正确解析参数。是因为map封装后并不是以&链接的。需要传递的参数和参数形式如下所示:
利用spring来作为后端框架,form-data利用LinkedMultiValueMap对象来包装多个参数,参数以key-value形式,中间以&连接。利用java代码的实现如下:
public ResponseData baseApi(String methodArg,String bizContent){
if(methodArg == null || methodArg.equals("")){
ResponseData responseData = new ResponseData(false,"the param methodArg is null","");
return responseData;
}
if(bizContent == null || bizContent.equals("")){
ResponseData responseData = new ResponseData(false,"the param bizContent is null","");
return responseData;
}
String apiUrl = Contants.publicUrl +"/"+ methodArg;
HttpHeaders headers = new HttpHeaders();
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
map.add("app_id", Contants.clientId);
map.add("method",methodArg);
map.add("format","json");
map.add("charset","utf-8");
map.add("timestamp",Contants.timeStamp);
map.add("token",Contants.accessToken);
map.add("biz_content",bizContent);
HttpEntity<MultiValueMap<String, String>> requestParams = new HttpEntity<>(map, headers);
ResponseEntity<String> response = restTemplate.postForEntity(apiUrl,requestParams,String.class);
String result =response.getBody();
JSONObject jsStr = JSONObject.parseObject(result);
String resultCode = jsStr.getString("code");
if(Integer.parseInt(resultCode)!= 0){
ResponseData responseData = new ResponseData(false,"error",result);
return responseData;
}
ResponseData responseData = new ResponseData(true,"success",result);
return responseData;
}
详细了解postman的几种调用方式可参考:
java实现postman中form-data传参方式_postman formdata_程可爱的博客-CSDN博客