Java HttpPost请求基于apache的httpclient

来源:互联网 发布:hao123解压软件 编辑:程序博客网 时间:2024/03/29 01:05
<span style="font-family: Arial, Helvetica, sans-serif;">/**</span>
* 发送 post请求访问本地应用并根据传递参数不同返回不同结果*/public static void post(String url, List<BasicNameValuePair> formparams) {// 创建默认的httpClient实例.CloseableHttpClient httpclient = HttpClients.createDefault();// 创建httppostHttpPost httppost = new HttpPost(url);// 创建参数队列UrlEncodedFormEntity uefEntity;try {uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");httppost.setEntity(uefEntity);System.out.println("executing request" + httppost.getURI());CloseableHttpResponse response = httpclient.execute(httppost);try {HttpEntity entity = response.getEntity();if (entity != null) {System.out.println("--------------------------------------");System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));System.out.println("--------------------------------------");}} finally {response.close();}} catch (ClientProtocolException e) {e.printStackTrace();} catch (UnsupportedEncodingException e1) {e1.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {// 关闭连接,释放资源try {httpclient.close();} catch (IOException e) {e.printStackTrace();}}}

0 0
原创粉丝点击