post请求改成body_接口调用post请求参数在body中

packagecom.ynhrm.common.utils;importcom.alibaba.fastjson.JSONObject;importlombok.Data;importorg.apache.http.Consts;importorg.apache.http.client.config.RequestConfig;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.DefaultHttpRequestRetryHandler;importorg.apache.http.impl.client.HttpClientBuilder;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.util.EntityUtils;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.Configuration;importorg.springframework.stereotype.Component;importjava.io.IOException;importjava.util.HashMap;importjava.util.Map;

@Component

@ConfigurationProperties("http.config")

@Datapublic classHttpUtils {static String url="http://localhost:9002/system/login";static String mobile="13294950520";static String password="123456";/*String url;

String mobile;

String password;*/String result="";publicString httpPost(){//CloseableHttpClient实现了HttpClient接口

CloseableHttpClient httpClient=HttpClients.createDefault();

HttpPost httpPost=newHttpPost(url);//创建HttpClientBuilder设置属性

HttpClientBuilder httpClientBuilder =HttpClientBuilder.create().setDefaultRequestConfig(RequestConfig.custom()

.setConnectionRequestTimeout(6000)

.setSocketTimeout(6000)

.setConnectTimeout(6000).build()).setRetryHandler(new DefaultHttpRequestRetryHandler(3, true));//设置请求头信息

Map map=new HashMap<>();

map.put("Accept","application/json, text/plain, */*");

map.put("Accept-Encoding","gzip, deflate");

map.put("Accept-Language","zh-CN,zh;q=0.9");

map.put("Connection","keep-alive");

map.put("Content-Type","application/json;charset=UTF-8");

map.put("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5024.400 QQBrowser/10.0.932.400");for (Map.Entryentry : map.entrySet()) {

httpPost.setHeader(entry.getKey(), entry.getValue());

}//传递参数为json数据

JSONObject jsonObject=newJSONObject();

jsonObject.put("mobile",mobile);

jsonObject.put("password",password);//创建指定内容和编码的字符串实体类

StringEntity entity=newStringEntity(jsonObject.toString(), Consts.UTF_8);//设置请求参数

httpPost.setEntity(entity);//创建HttpClient对象,CloseableHttpClient实例的生成器

httpClient=httpClientBuilder.build();try{//发送HttpPost请求,获取返回值

CloseableHttpResponse response=httpClient.execute(httpPost);

result=EntityUtils.toString(response.getEntity(), Consts.UTF_8);

}catch(Exception e) {

e.printStackTrace();

}finally{try{//释放资源

httpClient.close();

}catch(Exception e) {

e.printStackTrace();

}

}returnresult;

}public static voidmain(String[] args) {

HttpUtils httpUtils= newHttpUtils();

String s=httpUtils.httpPost();

System.out.println(s);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值