返回到文章

采纳

编辑于 4年前

java使用http发送数据请求,怎样保证数据不丢失?

java

在使用httpclient发送post请求时,怎样保证数据安全可靠传输到对方服务器

springboot,数据传输到对方服务器

@Autowired
private CloseableHttpClient httpClient;

@Autowired
private RequestConfig config;
public String doPost(String url) throws Exception{
    //声明http请求
    HttpPost httpPost = new HttpPost(url);
    //装载配置信息
    httpPost.setConfig(config);
    //发起请求
    CloseableHttpResponse response = this.httpClient.execute(httpPost);
    //判断状态码是否为200
    if(response.getStatusLine().getStatusCode() == 200){
        //返回响应体内容
        return EntityUtils.toString(response.getEntity(),"UTF-8");
    }
    return null;
}