Exemplo n.º 1
0
 /**
  * POSTs the variables and returns the body
  *
  * @param url - fully qualified and encoded URL to post to
  * @param params
  * @return
  */
 public String doPost(String url, Map<String, String> params)
     throws com.ettrema.httpclient.HttpException, NotAuthorizedException, ConflictException,
         BadRequestException, NotFoundException {
   notifyStartRequest();
   HttpPost m = new HttpPost(url);
   List<NameValuePair> formparams = new ArrayList<NameValuePair>();
   for (Entry<String, String> entry : params.entrySet()) {
     formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
   }
   UrlEncodedFormEntity entity;
   try {
     entity = new UrlEncodedFormEntity(formparams);
   } catch (UnsupportedEncodingException ex) {
     throw new RuntimeException(ex);
   }
   m.setEntity(entity);
   try {
     ByteArrayOutputStream bout = new ByteArrayOutputStream();
     int res = Utils.executeHttpWithStatus(client, m, bout);
     Utils.processResultCode(res, url);
     return bout.toString();
   } catch (HttpException ex) {
     throw new RuntimeException(ex);
   } catch (IOException ex) {
     throw new RuntimeException(ex);
   } finally {
     notifyFinishRequest();
   }
 }
Exemplo n.º 2
0
  /**
   * Retrieve the bytes at the specified path.
   *
   * @param path - encoded but not fully qualified. Must NOT be slash prefixed as it will be
   *     appended to the host's url
   * @return
   * @throws com.ettrema.httpclient.HttpException
   */
  public synchronized byte[] get(String path)
      throws com.ettrema.httpclient.HttpException, NotAuthorizedException, BadRequestException,
          ConflictException, NotFoundException {
    String url = this.encodedUrl() + path;
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
      transferService.get(
          url,
          new StreamReceiver() {

            @Override
            public void receive(InputStream in) {
              try {
                IOUtils.copy(in, out);
              } catch (IOException ex) {
                throw new RuntimeException(ex);
              }
            }
          },
          null,
          null);
    } catch (CancelledException ex) {
      throw new RuntimeException("Should never happen because no progress listener is set", ex);
    }
    return out.toByteArray();
  }