Exemple #1
0
 /**
  * Performs an HTTP GET request to the specified url.
  *
  * @param url The web address to post the request to
  * @return The result of the request
  * @throws Exception
  */
 public static String executeHttpGet(String url, int length) throws Exception {
   InputStream in = null;
   try {
     HttpClient client = getHttpClient();
     HttpGet request = new HttpGet();
     request.setURI(new URI(url));
     HttpResponse response = client.execute(request);
     in = response.getEntity().getContent();
     String result = HttpReaders.readIt(in, length);
     in.close();
     return result;
   } finally {
     if (in != null) {
       try {
         in.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   }
 }
Exemple #2
0
  /**
   * Performs an HTTP Post request to the specified url with the specified parameters.
   *
   * @param url The web address to post the request to
   * @param postParameters The parameters to send via the request
   * @return The result of the request
   * @throws Exception
   */
  public static String executeHttpPost(String url, List<NameValuePair> postParameters, int length)
      throws Exception {
    InputStream in = null;
    try {
      HttpClient client = getHttpClient(); // new DefaultHttpClient();
      HttpPost request = new HttpPost(url);
      UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
      request.setEntity(formEntity);
      HttpResponse response = client.execute(request);
      in = response.getEntity().getContent();
      String res = HttpReaders.readIt(in, length);
      in.close();

      return res;
    } finally {
      if (in != null) {
        try {
          in.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }