其他分享
首页 > 其他分享> > 【二】HttpClient4.3.1 HttpPost

【二】HttpClient4.3.1 HttpPost

作者:互联网

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

使用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

标签:httpEntity,HttpPost,closeableHttpClient,entity,HttpClient4.3,httpPost,new
来源: https://blog.csdn.net/weixin_34405332/article/details/92072571