Package org.apache.commons.httpclient.methods

Examples of org.apache.commons.httpclient.methods.PostMethod


      {
         return new GetMethod(url);
      }
      else if ("POST".equals(restVerb))
      {
         return new PostMethod(url);
      }
      else if ("DELETE".equals(restVerb))
      {
         return new DeleteMethod(url);
      }
      else
      {
         final String verb = restVerb;
         return new PostMethod(url)
         {
            @Override
            public String getName()
            {
               return verb;
View Full Code Here


         throw new RuntimeException("You cannot send both form parameters and an entity body");

      if (!request.getFormParameters().isEmpty())
      {
         commitHeaders(request, httpMethod);
         PostMethod post = (PostMethod) httpMethod;

         for (Map.Entry<String, List<String>> formParam : request.getFormParameters().entrySet())
         {
            List<String> values = formParam.getValue();
            for (String value : values)
            {
               post.addParameter(formParam.getKey(), value);
            }
         }
      }
      else if (request.getBody() != null)
      {
         if (!(httpMethod instanceof EntityEnclosingMethod))
            throw new RuntimeException("A GET request cannot have a body.");
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         request.writeRequestBody(request.getHeadersAsObjects(), baos);
         commitHeaders(request, httpMethod);
         ClientRequestEntity requestEntity = new ClientRequestEntity(request.getBodyContentType().toString(), baos.toByteArray());
         EntityEnclosingMethod post = (EntityEnclosingMethod) httpMethod;
         post.setRequestEntity(requestEntity);
      }
      else
      {
         commitHeaders(request, httpMethod);
      }
View Full Code Here

   @Test
   public void testAsynch() throws Exception
   {
      HttpClient client = new HttpClient();
      {
         PostMethod method = new PostMethod("http://localhost:9095/resource?asynch=true");
         method.setRequestEntity(new StringRequestEntity("content", "text/plain", null));
         int status = client.executeMethod(method);
         Assert.assertEquals(Response.Status.ACCEPTED.getStatusCode(), status);
         String jobUrl1 = method.getResponseHeader(HttpHeaders.LOCATION).getValue();

         GetMethod get = new GetMethod(jobUrl1);
         status = client.executeMethod(get);
         Assert.assertEquals(Response.Status.ACCEPTED.getStatusCode(), status);

         Thread.sleep(1500);
         status = client.executeMethod(get);
         Assert.assertEquals(Response.Status.OK.getStatusCode(), status);
         Assert.assertEquals(get.getResponseBodyAsString(), "content");

         method.releaseConnection();
      }
   }
View Full Code Here

    }
   
    public void sendMessage(String callbackURI, String messageSenderId, String message) {
        HttpClient client = new HttpClient();
        try {
            PostMethod method = new PostMethod(getPushMessageURL(callbackURI, messageSenderId));
            method.setRequestEntity(new StringRequestEntity(message, "text/plain", "UTF-8"));
            int status = client.executeMethod(method);
            if (HttpResponseCodes.SC_OK != status) {
               throw new RuntimeException("Message can not be delivered to subscribers");
            }
        } catch (Exception ex)
View Full Code Here

   }
  
   public String getSharedSecret(String consumerKey) throws Exception
   {
      HttpClient client = new HttpClient();
      PostMethod method = new PostMethod(ConsumerRegistrationURL);
      method.addParameter(OAuth.OAUTH_CONSUMER_KEY, consumerKey);
      int status = client.executeMethod(method);
      if (HttpResponseCodes.SC_OK != status) {
          throw new RuntimeException("Registration failed");
      }
      // check that we got all tokens
      Map<String, String> response = OAuth.newMap(OAuth.decodeForm(method.getResponseBodyAsString()));
      String secret = response.get("xoauth_consumer_secret");
      if (secret == null) {
          throw new RuntimeException("No secret available");
      }
      return secret;
View Full Code Here

    }

    @Test
    public void testPost() throws Exception {

        PostMethod post = new PostMethod(TEST_URI);

        MyObject o1 = YamlResource.createMyObject();

        String s1 = new Yaml().dump(o1);

        post.setRequestEntity(new StringRequestEntity(s1, "text/x-yaml", "utf-8"));

        client.executeMethod(post);

        Assert.assertEquals(200, post.getStatusCode());

        Assert.assertEquals("text/x-yaml", post.getResponseHeader("Content-Type").getValue());

        Assert.assertEquals(s1, post.getResponseBodyAsString());

    }
View Full Code Here

    }

    @Test
    public void testBadPost() throws Exception {

        PostMethod post = new PostMethod(TEST_URI);

        post.setRequestEntity(new StringRequestEntity("---! bad", "text/x-yaml", "utf-8"));

        client.executeMethod(post);

        Assert.assertEquals(500, post.getStatusCode());

    }
View Full Code Here

   }
  
   public String confirmAuthorization(String url) throws Exception
   {
      HttpClient client = new HttpClient();
      PostMethod method = new PostMethod(url);
      Base64 base64 = new Base64();
      String base64Credentials = new String(base64.encode("admin:admin".getBytes()));
      method.addRequestHeader(new Header("Authorization", "Basic " + base64Credentials));
     
      int status = client.executeMethod(method);
      if (302 != status) {
          throw new RuntimeException("Initiation failed");
      }
      // check that we got all tokens
      String callbackURI = method.getResponseHeader("Location").getValue();
      if (callbackURI == null) {
          throw new RuntimeException("Callback failed");
      }
      return callbackURI;
   }
View Full Code Here

   }
  
   public String setCallback(String url) throws Exception
   {
      HttpClient client = new HttpClient();
      PostMethod method = new PostMethod(url);
      int status = client.executeMethod(method);
      if (200 != status) {
          throw new RuntimeException("Service failed");
      }
      return method.getResponseBodyAsString();
   }
View Full Code Here

              null, new MediaType("ApplIcAtion", "STufF"));
      Assert.assertNotNull(messageBodyReader);
      Assert.assertNotNull(messageBodyReader.getClass());
      Assert.assertEquals(StuffProvider.class, messageBodyReader.getClass());
      HttpClient client = new HttpClient();
      PostMethod post = new PostMethod(generateURL("/stuff"));
      post.setRequestEntity(new StringRequestEntity("bill", "Application/Stuff", null));
      int status = client.executeMethod(post);
      Assert.assertEquals(204, status);


   }
View Full Code Here

TOP

Related Classes of org.apache.commons.httpclient.methods.PostMethod

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.