Press "Enter" to skip to content

HttpClient POST请求示例代码

老项目 HTTP 请求使用了一些已经过期(废弃)的类或方法,导致IDE提示 'org.apache.http.impl.client.DefaultHttpClient' is deprecated等。

新代码稍微更新一下,不再使用废弃的类和方法。上一篇《HttpClient 4 简介》已经介绍了相关使用,这里提供完整DEMO。

Gradle 依赖

compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.4.1'
compile group: 'org.apache.httpcomponents', name: 'httpclient-cache', version: '4.2.5'
compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.1'
compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.2.5'
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.1'

示例代码

package Http;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
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.util.EntityUtils;
import java.io.IOException;
import java.util.UUID;

public class HttpPostTest {

    public static void main(String[] args) {
        post();
    }

    private static final String POST_URL = "http://httpbin.org/post";

    private static void post() {
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

        // 设置超时时间,单位毫秒
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectionRequestTimeout(1000)
                .setConnectTimeout(1000)
                .setSocketTimeout(1000)
                .build();

        try {
            // 发起POST请求
            HttpPost request = new HttpPost(POST_URL);
            JSONObject requestParams = new JSONObject();
            request.addHeader("Content-type", "application/json");

            // 请求参数
            UUID uuid = UUID.randomUUID();
            requestParams.put("uuid", uuid.toString());
            StringEntity requestEntity = new StringEntity(requestParams.toJSONString(), "UTF-8");
            request.setEntity(requestEntity);
            request.setConfig(requestConfig);
            HttpResponse response = closeableHttpClient.execute(request);
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("statusCode:"+statusCode);
            HttpEntity entity = response.getEntity();
            String content = EntityUtils.toString(entity);
            System.out.println("response:"+content);
            EntityUtils.consume(entity);

        } catch (IOException e) {
            System.out.println("IOException"+e);
        } catch (Exception e) {
            System.out.println("exceprion"+e);
        } finally {
            try {
                //关闭流并释放资源
                closeableHttpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}