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