【二】HttpClient4.3.1 HttpPost

2019独角兽企业重金招聘Python工程师标准>>> 【二】HttpClient4.3.1 HttpPost

使用HttpClient Post提交数据,详细代码注释

public static void main(String args[]) {
        //创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        //HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

        HttpPost httpPost = new HttpPost("http://tutor.sturgeon.mopaas.com/tutor/search");
        httpPost.setConfig(DEFAULT);
        // 创建参数队列
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("searchText", "英语"));

        UrlEncodedFormEntity entity;
        try {
            entity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httpPost.setEntity(entity);

            HttpResponse httpResponse;
            //post请求
            httpResponse = closeableHttpClient.execute(httpPost);

            //getEntity()
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                //打印响应内容
                System.out.println("response:" + EntityUtils.toString(httpEntity, "UTF-8"));
            }
            //释放资源
            closeableHttpClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



转载于:https://my.oschina.net/tanweijie/blog/195286

上一篇:CloseableHttpClient 发送post请求


下一篇:RocketMQ安装部署-最小化版本适用于开发测试环境