示例#1
0
 protected <T> T put(String uri, HttpEntity httpEntity, TypeReference<T> clazz)
     throws IOException {
   HttpPut httpPut = new HttpPut();
   httpPut.setURI(getAbsoluteURI(uri));
   httpPut.setEntity(httpEntity);
   HttpResponse response = executeRequest(httpPut);
   return unmarshallResponse(clazz, response);
 }
  /**
   * Create http request object
   *
   * @param method
   * @param uri
   * @param headers
   * @param entity
   * @return
   */
  public static HttpRequest createHttpRequest(
      HttpMethodType method, URI uri, Header[] headers, HttpEntity entity) {

    HttpRequest request = null;

    if (method == HttpMethodType.GET) {
      HttpGet get = new HttpGet();
      get.setURI(uri);
      request = get;
    } else if (method == HttpMethodType.TRACE) {
      HttpTrace trace = new HttpTrace();
      trace.setURI(uri);
      request = trace;
    } else if (method == HttpMethodType.OPTIONS) {
      HttpOptions options = new HttpOptions();
      options.setURI(uri);
      request = options;
    } else if (method == HttpMethodType.HEAD) {
      HttpHead head = new HttpHead();
      head.setURI(uri);
      request = head;
    } else if (method == HttpMethodType.POST) {
      HttpPost post = new HttpPost();
      post.setURI(uri);
      post.setEntity(entity);
      request = post;
    } else if (method == HttpMethodType.PUT) {
      HttpPut put = new HttpPut();
      put.setURI(uri);
      put.setEntity(entity);
      request = put;
    }

    if (request != null) {
      if (headers != null) {
        request.setHeaders(headers);
      }
    }

    return request;
  }