Exemple #1
0
 /**
  * httpClient的GET请求 返回ResponseBody
  *
  * @param url
  * @param paramsMap
  * @return
  * @throws Exception
  */
 public static String get(String url, Map<String, String> paramsMap) throws Exception {
   String result = null;
   HttpClient httpClient = HttpClientUtil.initHttpClient();
   GetMethod getMethod = HttpClientUtil.getMethod(url, paramsMap);
   // 请求成功
   int status = httpClient.executeMethod(getMethod);
   if (status == 200) {
     result = getMethod.getResponseBodyAsString();
   }
   return result;
 }
Exemple #2
0
 /**
  * httpClient的POST请求 返回ResponseBody
  *
  * @param url
  * @param paramsMap
  * @return
  * @throws Exception
  */
 public static String post(String url, Map<String, String> paramsMap) throws Exception {
   String result = null;
   HttpClient httpClient = HttpClientUtil.initHttpClient();
   PostMethod postMethod = new PostMethod(url);
   postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
   NameValuePair[] params = new NameValuePair[paramsMap.keySet().size()];
   Iterator<String> it = paramsMap.keySet().iterator();
   int i = 0;
   String key = "";
   while (it.hasNext()) {
     key = it.next();
     params[i] = new NameValuePair(key, paramsMap.get(key));
     i++;
   }
   postMethod.setQueryString(params);
   postMethod.releaseConnection();
   // 请求成功
   int status = httpClient.executeMethod(postMethod);
   if (status == 200) {
     result = postMethod.getResponseBodyAsString();
   }
   return result;
 }