http发送get/post请求,普通传参

http 请求工具类,需引用合适的jar
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * description: http 请求工具类
 */
public class HttpClientUtil {
    public static final String reqSystemID = "aweb";
    public static final String reqSystemName = "aweb端";
/**
     * description: 发送http get 请求
     * @param url url
     * @return java.lang.String
     */
    public static String doHttpGet(String url) {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        //创建HttpGet对象
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        try {
            //执行GET请求
            response = httpClient.execute(httpGet);
//            System.out.println(response.getStatusLine());
            //获取响应实体
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            try {
                response.close();
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * description: 发送http post 请求
     * @param url
     * @param json josn参数
     * @return java.lang.String
     */
    public static String doHttpPost(String url, String json) {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        //创建HttpPost对象
        HttpPost post = new HttpPost(url);
        //设置POST请求传递参数
        try {
            post.setHeader(HTTP.CONTENT_TYPE, "application/json");
            post.setEntity(new StringEntity(json,
                            StandardCharsets.UTF_8));

        } catch (Exception e) {
            e.printStackTrace();
        }
        //执行请求并处理响应
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(post);
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity,"UTF-8");
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
/**
* description: 发送http post 请求
* @param url
* @param String josn参数
 * @param json headerInfo参数
 * @return java.lang.String
*/
public static String doHttpPost(String url, String json, JSONObject headerInfo) {
//获取时间、流水
String time = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
int random = (int) (Math.random()*1000);
String serialNo = time + String.format("%03d",random);
//创建HttpClient对象
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//创建HttpPost对象
HttpPost post = new HttpPost(url);
//设置POST请求传递参数
try {
//获取当前用户信息
String operatorID = headerInfo.getString("name");
String operatorName = headerInfo.getString("nickname");
//设置http请求头数据
post.setHeader("operatorID", operatorID);
post.setHeader("operatorName", URLEncoder.encode(operatorName,"utf-8"));
post.setHeader("reqSystemID", reqSystemID);
post.setHeader("reqSystemName", URLEncoder.encode(reqSystemName,"utf-8"));  //解决中文乱码
post.setHeader("reqSystemDate", time.substring(0,8));
post.setHeader("reqSystemTime", time.substring(8,14));
post.setHeader("reqSystemSerialNO", serialNo);
post.setHeader(HTTP.CONTENT_TYPE, "application/json");

post.setEntity(new StringEntity(json, StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
}
//执行请求并处理响应
CloseableHttpResponse response = null;
try {
response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity,"UTF-8");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}

 

posted @ 2022-08-21 21:07  借你耳朵说爱你  阅读(335)  评论(0编辑  收藏  举报