//超时时间
private static final int ConnectionRequestTimeout = 5000;//从连接池中获取连接的超时时间
private static final int ConnectTimeout = 60000;//与服务器连接超时时间:httpclient会创建一个异步线程用以创建socket连接,此处设置该socket的连接超时时间
private static final int SocketTimeout = 60000;//socket读数据超时时间:从服务器获取响应数据的超时时间
private static final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(ConnectionRequestTimeout).setConnectTimeout(ConnectTimeout).setSocketTimeout(SocketTimeout).build();
//发送post请求,参数为键值对
public static Map<String, Object> sendPost(String url, String[] name, String[] value, String[] herderName, String[] headerValue) throws IOException {
// 获取默认的请求客户端,CloseableHttpClient是HttpClient的实例
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
// 设置超时
httpPost.setConfig(requestConfig);
//添加herder信息
for (int i = 0; i < herderNam
java利用org.apache.http.client.HttpClient发送3种post请求(参数为json、参数为键值对、参数为文件)
最新推荐文章于 2024-06-13 15:14:53 发布