【二】HttpClient4.3.1 HttpPost
作者:互联网
原文链接:https://my.oschina.net/tanweijie/blog/195286
使用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,HttpClient4.3,httpPost,new,HttpClientBui 来源: https://blog.csdn.net/chezhi1895/article/details/100639270