Esempio n. 1
0
 public Response<String> fetch(String requestURLPath) {
   try {
     HttpGet request = new HttpGet(requestURLPath);
     request.setHeader("Authorization", "Basic " + getEncodedCredentials());
     String responseContent = IOUtils.toString(httpClient.fetchContent(request));
     return new Response<String>(ResponseStatus.success, responseContent);
   } catch (Exception e) {
     System.out.println("failed: " + e.getMessage());
     return new Response<String>(ResponseStatus.failure, null);
   }
 }
Esempio n. 2
0
  public Response<String> post(String postURLPath, String jsonPayload) {
    try {
      HttpPost httpPost = new HttpPost(postURLPath);
      httpPost.setHeader("Authorization", "Basic " + getEncodedCredentials());

      StringEntity entity = new StringEntity(jsonPayload, HTTP.UTF_8);
      entity.setContentType("application/json; charset=utf-8");
      httpPost.setEntity(entity);

      HttpResponse response = httpClient.postContent(httpPost);
      System.out.println("HTTP status code:" + response.getStatusLine().getStatusCode());

      ResponseStatus responseStatus =
          response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED
              ? ResponseStatus.success
              : ResponseStatus.failure;
      response.getEntity().consumeContent();
      return new Response<String>(responseStatus, null);
    } catch (Exception e) {
      // logWarn(e.toString());
      return new Response<String>(ResponseStatus.failure, null);
    }
  }