Package org.apache.http.client.methods

Examples of org.apache.http.client.methods.HttpPost


  }

  public <T extends Resource> T post() {
    //    HttpPost request = (!hasFiles()) || isMultiPart ? composePostRequest(getBaseUri(), parameters) :
    //      composeMultiPartFormRequest(getBaseUri(), parameters, files);
    HttpPost request = (hasFiles() || isMultiPart) ? composeMultiPartFormRequest(getBaseUri(), parameters, files) :
      composePostRequest(getBaseUri(), parameters);
    buildHeaders(request);
    return requestor.request(request);
  }
View Full Code Here


      throw MechanizeExceptionFactory.newException(e);
    }
  }

  private HttpPost composePostRequest(final String uri, final Parameters parameters) {
    HttpPost request = new HttpPost(uri);
    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    for(Parameter param : parameters)
      if(param.isSingleValue())
        formparams.add(new BasicNameValuePair(param.getName(), param.getValue()));
      else
        for(String value : param.getValues())
          formparams.add(new BasicNameValuePair(param.getName(), value));
    try {
      UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
      request.setEntity(entity);
    } catch (UnsupportedEncodingException e) {
      throw MechanizeExceptionFactory.newException(e);
    }

    return request;
View Full Code Here

    return request;
  }

  private HttpPost composeMultiPartFormRequest(final String uri, final Parameters parameters, final Map<String, ContentBody> files) {
    HttpPost request = new HttpPost(uri);
    MultipartEntity multiPartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    try {
      Charset utf8 = Charset.forName("UTF-8");
      for(Parameter param : parameters)
        if(param.isSingleValue())
          multiPartEntity.addPart(param.getName(), new StringBody(param.getValue(), utf8));
        else
          for(String value : param.getValues())
            multiPartEntity.addPart(param.getName(), new StringBody(value, utf8));
    } catch (UnsupportedEncodingException e) {
      throw MechanizeExceptionFactory.newException(e);
    }

    List<String> fileNames = new ArrayList<String>(files.keySet());
    Collections.sort(fileNames);
    for(String name : fileNames) {
      ContentBody contentBody = files.get(name);
      multiPartEntity.addPart(name, contentBody);
    }
    request.setEntity(multiPartEntity);
    return request;
  }
View Full Code Here

  }

  @Before
  public void setUp() {
    httpClient = new DefaultHttpClient();
    postRequest = new HttpPost("http://0.0.0.0:" + selectedPort);
  }
View Full Code Here

    while (statusCode != HttpStatus.SC_OK && triesCount < serversList.size()) {
      triesCount++;
      String host = serversList.get();
      String url = host + "/" + BULK_ENDPOINT;
      HttpPost httpRequest = new HttpPost(url);
      httpRequest.setEntity(new StringEntity(entity));
      response = httpClient.execute(httpRequest);
      statusCode = response.getStatusLine().getStatusCode();
      logger.info("Status code from elasticsearch: " + statusCode);
      if (response.getEntity() != null)
        logger.debug("Status message from elasticsearch: " + EntityUtils.toString(response.getEntity(), "UTF-8"));
View Full Code Here

    assertTrue(StringUtils.isNotBlank(accessTokenResponse.getTokenType()));
    assertTrue(accessTokenResponse.getExpiresIn() > 0);

    String tokenUrl = String.format("%s/oauth2/token", baseUrl());

    final HttpPost tokenRequest = new HttpPost(tokenUrl);
   
    /*
     * Now make a request for a new AccessToken based on the refreshToken
     */
    String postBody = String.format("grant_type=%s&refresh_token=%s&state=%s",
            OAuth2Validator.GRANT_TYPE_REFRESH_TOKEN, accessTokenResponse.getRefreshToken(), "dummy");

    tokenRequest.setEntity(new ByteArrayEntity(postBody.getBytes()));
    tokenRequest.addHeader("Authorization", AuthorizationCodeTestIT.authorizationBasic(clientId, secret));

    tokenRequest.addHeader("Content-Type", "application/x-www-form-urlencoded");

    HttpResponse tokenHttpResponse = new DefaultHttpClient().execute(tokenRequest);
    final InputStream responseContent = tokenHttpResponse.getEntity().getContent();
    String responseAsString = IOUtils.toString(responseContent);

View Full Code Here

    Map<String, String> params = getParamsFromUri(uri);
    String authorizationCode = params.get("code");
    authorizationResponseState = params.get("state");
    LOG.debug("URL: {}, state: {}", uri, authorizationResponseState);

    final HttpPost tokenRequest = new HttpPost(oauthServerBaseUrl + "/oauth2/token");
    String postBody = getPostBody(authorizationCode, grantType);

    tokenRequest.setEntity(new ByteArrayEntity(postBody.getBytes()));
    tokenRequest.addHeader("Authorization", AuthorizationCodeTestIT.authorizationBasic(clientId, secret));
    tokenRequest.addHeader("Content-Type", "application/x-www-form-urlencoded");

    HttpResponse tokenHttpResponse = new DefaultHttpClient().execute(tokenRequest);
    final InputStream responseContent = tokenHttpResponse.getEntity().getContent();
    String responseAsString = new Scanner(responseContent).useDelimiter("\\A").next();
    responseContent.close();
View Full Code Here

    assertEquals("The client has no permisssion for client credentials", response.get("error_description"));
  }

  private InputStream performClientCredentialTokenPost(String username, String password) throws IOException {
    String tokenUrl = String.format("%s/oauth2/token", baseUrl());
    final HttpPost tokenRequest = new HttpPost(tokenUrl);
    String postBody = String.format("grant_type=%s", OAuth2Validator.GRANT_TYPE_CLIENT_CREDENTIALS );

    tokenRequest.setEntity(new ByteArrayEntity(postBody.getBytes()));
    tokenRequest.addHeader("Authorization", authorizationBasic(username, password));
    tokenRequest.addHeader("Content-Type", "application/x-www-form-urlencoded");

    HttpResponse tokenHttpResponse = new DefaultHttpClient().execute(tokenRequest);
    return tokenHttpResponse.getEntity().getContent();
  }
View Full Code Here

  private static Map<String, Object> postJSON(String url, String body, Integer connectionTimeout, Integer soTimeout)
  {
    Map<String, Object> map;
    try
    {
      HttpPost httppost = new HttpPost(url);
      if (body != null)
      {
        httppost.setHeader("Content-Type", "application/json;charset=UTF-8");
        httppost.setEntity(new StringEntity(body, "UTF-8"));
      }
      map = executeMethod(httppost, url, body, connectionTimeout, soTimeout);
    }
    catch (UnsupportedEncodingException e)
    {
View Full Code Here

  private static Map<String, Object> postJSON(String url, String body, Integer connectionTimeout, Integer soTimeout)
  {
    Map<String, Object> map;
    try
    {
      HttpPost httppost = new HttpPost(url);
      if (body != null)
      {
        httppost.setHeader("Content-Type", "application/json;charset=UTF-8");
        httppost.setEntity(new StringEntity(body, "UTF-8"));
      }
      map = executeMethod(httppost, url, body, connectionTimeout, soTimeout);
    }
    catch (UnsupportedEncodingException e)
    {
View Full Code Here

TOP

Related Classes of org.apache.http.client.methods.HttpPost

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.