0


JAVA实现postman form-data和raw方式请求数据的代码

java raw方式 的实现代码如下:

Url url = new Url("");

HttpURLConnection connection = (HttpURLConnection ) url.oepnConnection();

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setUseCaches(false);

connection.setInstanceFollowRedirects(true);

connection.setRequestMethod("POST");

connection.setRequestProperty("Accept","application/json,charset=utf-8");

connection.connect();

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");

out.append(params);

out.flush();

out.close();

BufferedReader reader = new BufferedReader(new InputStreanReader(connect.getInputStream(),"UTF-8");

String str = "";

while((line = reader.readLine())!= null){

res += line;

}

reader.close();

java form-data方式 的实现代码如下:

LinkedMultiValueMap<String,Object> map= new LinkedMultiValueMap<String,Object>();

map.add("",new ArrayList);//放list对象

map.add("",new String("111"));//放string

RestTemplate template = new RestTemplate();

HttpHeaders headers = new HttpHeaders();

HttpEntity<LinkedMultiValueMap<String,Object>> request = HttpEntity<LinkedMultiValueMap<String,Object>>(map,headers);

ResponseEntity responseEntity = template.postEntity(url,request,String.class);

HttpStatus statusCode = responseEntity .getStatusCode();

String resultString = responseEntity.getBody();

亲测,完全ok。

标签: java 开发语言

本文转载自: https://blog.csdn.net/weixin_39334258/article/details/124769647
版权归原作者 weixin_39334258 所有, 如有侵权,请联系我们删除。

“JAVA实现postman form-data和raw方式请求数据的代码”的评论:

还没有评论