Package org.apache.http.client.methods

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


    headers.add(new BasicHeader("Connection", "Close"));
    HttpParams params = new BasicHttpParams();
    params.setParameter("http.default-headers", headers);

    HttpClient httpclient = new DefaultHttpClient(params);
    HttpPost httppost = new HttpPost("http://localhost:" + PORT + "/post");
    HttpResponse response = httpclient.execute(httppost);

    assertNotNull(response);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
View Full Code Here


  @Test
  public void smallHttpPostBodyWithUnusualCharactersTest() throws ClientProtocolException, IOException {
    final String body = "Räger Schildmäijår";
   
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://localhost:" + PORT + "/echo");
    httppost.setEntity(new StringEntity(body))// HTTP 1.1 says that the default charset is ISO-8859-1
    HttpResponse response = httpclient.execute(httppost)
   
    assertNotNull(response);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
View Full Code Here

      {
         return new HttpGet(url);
      }
      else if ("POST".equals(restVerb))
      {
         return new HttpPost(url);
      }
      else if ("DELETE".equals(restVerb))
      {
         return new HttpDelete(url);
      }
      else
      {
         final String verb = restVerb;
         return new HttpPost(url)
         {
            @Override
            public String getMethod()
            {
               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);
         HttpPost post = (HttpPost) httpMethod;

         List<NameValuePair> formparams = new ArrayList<NameValuePair>();

         for (Map.Entry<String, List<String>> formParam : request.getFormParameters().entrySet())
         {
            List<String> values = formParam.getValue();
            for (String value : values)
            {
               formparams.add(new BasicNameValuePair(formParam.getKey(), value));
            }
         }

         UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
         post.setEntity(entity);
      }
      else if (request.getBody() != null)
      {
         if (httpMethod instanceof HttpGet) throw new RuntimeException("A GET request cannot have a body.");

         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         try
         {
            request.writeRequestBody(request.getHeadersAsObjects(), baos);
            ByteArrayEntity entity = new ByteArrayEntity(baos.toByteArray())
            {
               @Override
               public Header getContentType()
               {
                  return new BasicHeader("Content-Type", request.getBodyContentType().toString());
               }
            };
            HttpPost post = (HttpPost) httpMethod;
            commitHeaders(request, httpMethod);
            post.setEntity(entity);
         }
         catch (IOException e)
         {
            throw new RuntimeException(e);
         }
View Full Code Here

     * @throws IOException
     */
    public void POST(final String uri, final InputStream instream, final long length) throws IOException {
      if (this.currentRequest != null) throw new IOException("Client is in use!");
        final MultiProtocolURI url = new MultiProtocolURI(uri);
        final HttpPost httpPost = new HttpPost(url.toNormalform(true, false, true, false));
        String host = url.getHost();
        if (host == null) host = "127.0.0.1";
        setHost(host); // overwrite resolved IP, needed for shared web hosting DO NOT REMOVE, see http://en.wikipedia.org/wiki/Shared_web_hosting_service
        final NonClosingInputStreamEntity inputStreamEntity = new NonClosingInputStreamEntity(instream, length);
      // statistics
      this.upbytes = length;
      httpPost.setEntity(inputStreamEntity);
      this.currentRequest = httpPost;
      execute(httpPost);
    }
View Full Code Here

     * @param usegzip if the body should be gzipped
     * @return response body
     * @throws IOException
     */
    public byte[] POSTbytes(final MultiProtocolURI url, final String vhost, final Map<String, ContentBody> post, final boolean usegzip) throws IOException {
      final HttpPost httpPost = new HttpPost(url.toNormalform(true, false, true, false));

        setHost(vhost); // overwrite resolved IP, needed for shared web hosting DO NOT REMOVE, see http://en.wikipedia.org/wiki/Shared_web_hosting_service
      if (vhost == null) setHost("127.0.0.1");

        final MultipartEntity multipartEntity = new MultipartEntity();
        for (final Entry<String,ContentBody> part : post.entrySet())
            multipartEntity.addPart(part.getKey(), part.getValue());
        // statistics
        this.upbytes = multipartEntity.getContentLength();

        if (usegzip) {
            httpPost.setEntity(new GzipCompressingEntity(multipartEntity));
        } else {
            httpPost.setEntity(multipartEntity);
        }

        return getContentBytes(httpPost, Long.MAX_VALUE);
    }
View Full Code Here

     * @return content bytes
     * @throws IOException
     */
    public byte[] POSTbytes(final String uri, final InputStream instream, final long length) throws IOException {
        final MultiProtocolURI url = new MultiProtocolURI(uri);
        final HttpPost httpPost = new HttpPost(url.toNormalform(true, false, true, false));
        String host = url.getHost();
        if (host == null) host = "127.0.0.1";
        setHost(host); // overwrite resolved IP, needed for shared web hosting DO NOT REMOVE, see http://en.wikipedia.org/wiki/Shared_web_hosting_service

        final InputStreamEntity inputStreamEntity = new InputStreamEntity(instream, length);
        // statistics
        this.upbytes = length;
        httpPost.setEntity(inputStreamEntity);
        return getContentBytes(httpPost, Long.MAX_VALUE);
    }
View Full Code Here

    @Override
    public void run() {
      try {
        final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        final HttpPost req = new HttpPost("http://datacleaner.eobjects.org/ws/user_action");
        nameValuePairs.add(new BasicNameValuePair("username", _username));
        nameValuePairs.add(new BasicNameValuePair("action", _action));
        nameValuePairs.add(new BasicNameValuePair("version", Main.VERSION));
        req.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse resp = HttpXmlUtils.getHttpClient().execute(req);
        InputStream content = resp.getEntity().getContent();
        String line = new BufferedReader(new InputStreamReader(content)).readLine();
        assert "success".equals(line);
View Full Code Here

  public static String getUrlContent(String url, Map<String, String> params) throws IOException {
    if (params == null) {
      params = Collections.emptyMap();
    }
    logger.info("getUrlContent({},{})", url, params);
    HttpPost method = new HttpPost(url);
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    for (Entry<String, String> entry : params.entrySet()) {
      nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
    }
    method.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String response = getHttpClient().execute(method, responseHandler);
    return response;
  }
View Full Code Here

  public XMLRPCClient(URI uri) {
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", new PlainSocketFactory(), 80));
    registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
   
    postMethod = new HttpPost(uri);
    postMethod.addHeader("Content-Type", "text/xml");
   
    // WARNING
    // I had to disable "Expect: 100-Continue" header since I had
    // two second delay between sending http POST request and POST body
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.