Пример #1
0
  public Either<IOException, HttpResponse> execUploadRequest(
      String path, List<FormBodyPart> parts, HTTPMethod method, String acceptType) {
    HttpEntityEnclosingRequestBase httpReq = null;
    String url = getUriForPath(path);
    if (method.equals(HTTPMethod.POST)) {
      httpReq = new HttpPost(url);
    } else if (method.equals(HTTPMethod.PUT)) {
      httpReq = new HttpPut(url);
    } else {
      throw new IllegalArgumentException("gotta be this or that: post or put");
    }
    MultipartEntity entity = new MultipartEntity();
    for (FormBodyPart part : parts) {
      entity.addPart(part);
    }
    httpReq.setEntity(entity);

    return execRequest(httpReq, acceptType);
  }
Пример #2
0
  public Either<IOException, HttpResponse> execNormalRequest(
      String path, Map<String, String> params, HTTPMethod method, String acceptType) {
    String url = getUriForPath(path);
    if (params == null) {
      params = new HashMap<String, String>();
    }
    HttpUriRequest httpReq = null;
    if (method.equals(HTTPMethod.GET)) {
      HttpGet get = new HttpGet(url);
      for (Map.Entry<String, String> param : params.entrySet()) {
        get.getParams().setParameter(param.getKey(), param.getValue());
      }
      httpReq = get;
    } else {
      HttpEntityEnclosingRequestBase postOrPut = null;
      if (method.equals(HTTPMethod.PUT)) {
        postOrPut = new HttpPut(url);
      } else {
        postOrPut = new HttpPost(url);
      }
      MultipartEntity entity = new MultipartEntity();

      List<NameValuePair> args = new ArrayList<NameValuePair>();
      for (Map.Entry<String, String> param : params.entrySet()) {
        args.add(new BasicNameValuePair(param.getKey(), param.getValue()));
      }
      try {
        postOrPut.setEntity(new UrlEncodedFormEntity(args));
      } catch (UnsupportedEncodingException e) {
        return new Left<IOException, HttpResponse>(e);
      }
      httpReq = postOrPut;
    }

    return execRequest(httpReq, acceptType);
  }