/** * 发起get请求 * @param url 请求地址 * @param headerMap 请求头 * @return */ public static String httpGet(String url,Map<String, Object> headerMap) { String result = null; HttpClient httpClient = new DefaultHttpClient(); try { HttpGet httpGet = new HttpGet(url); if (headerMap != null) { for (String key : headerMap.keySet()) { httpGet.addHeader(key, headerMap.get(key).toString()); } } HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); result = EntityUtils.toString(httpEntity, "UTF-8"); EntityUtils.consume(httpEntity); } catch (ClientProtocolException e) { log.error(e.getMessage()); } catch (IOException e) { log.error(e.getMessage()); } finally { httpClient.getConnectionManager().shutdown(); } return result; } /** * post请求 *
java实现HTTP协议中GET和POST方法
最新推荐文章于 2024-08-03 21:02:21 发布