Package org.apache.http.entity

Examples of org.apache.http.entity.ContentType


    public Request bodyForm(final Iterable <? extends NameValuePair> formParams, final Charset charset) {
        final List<NameValuePair> paramList = new ArrayList<NameValuePair>();
        for (NameValuePair param : formParams) {
            paramList.add(param);
        }
        final ContentType contentType = ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset);
        final String s = URLEncodedUtils.format(paramList, charset != null ? charset.name() : null);
        return bodyString(s, contentType);
    }
View Full Code Here


                    final HttpContext context) throws HttpException, IOException {
                HttpEntity responseEntity = null;
                if (request instanceof HttpEntityEnclosingRequest) {
                    final HttpEntity requestEntity = ((HttpEntityEnclosingRequest) request).getEntity();
                    if (requestEntity != null) {
                        final ContentType contentType = ContentType.getOrDefault(requestEntity);
                        if (ContentType.TEXT_PLAIN.getMimeType().equals(contentType.getMimeType())) {
                            responseEntity = new StringEntity(
                                    EntityUtils.toString(requestEntity), ContentType.TEXT_PLAIN);
                        }
                    }
                }
View Full Code Here

     * @throws IOException
     *             If there was an exception getting the entity's data.
     */
    public static List <NameValuePair> parse (
            final HttpEntity entity) throws IOException {
        final ContentType contentType = ContentType.get(entity);
        if (contentType != null && contentType.getMimeType().equalsIgnoreCase(CONTENT_TYPE)) {
            final String content = EntityUtils.toString(entity, Consts.ASCII);
            if (content != null && content.length() > 0) {
                Charset charset = contentType.getCharset();
                if (charset == null) {
                    charset = HTTP.DEF_CONTENT_CHARSET;
                }
                return parse(content, charset);
            }
View Full Code Here

     * @throws IOException
     *             If there was an exception getting the entity's data.
     */
    public static List <NameValuePair> parse (
            final HttpEntity entity) throws IOException {
        ContentType contentType = ContentType.get(entity);
        if (contentType != null && contentType.getMimeType().equalsIgnoreCase(CONTENT_TYPE)) {
            String content = EntityUtils.toString(entity, Consts.ASCII);
            if (content != null && content.length() > 0) {
                Charset charset = contentType != null ? contentType.getCharset() : null;
                if (charset == null) {
                    charset = HTTP.DEF_CONTENT_CHARSET;
                }
                return parse(content, charset);
            }
View Full Code Here

    String result = null;
    if (entity != null) {
      Charset charset = null;
      Header encoding = entity.getContentEncoding();
      if (encoding == null) {
        ContentType contentType = ContentType.get(entity);
        if (contentType != null) {
          charset = contentType.getCharset();
        }
      } else {
        charset = Charset.forName(encoding.getValue());
      }
      try {
View Full Code Here

                        int l;
                        while((l = instream.read(tmp)) != -1) {
                            Thread.sleep(1);
                            outstream.write(tmp, 0, l);
                        }
                        ContentType contentType = ContentType.getOrDefault(entity);
                        String charset = contentType.getCharset();
                        if (charset == null) {
                            charset = HTTP.DEFAULT_CONTENT_CHARSET;
                        }
                        content = new String(outstream.toByteArray(), charset);
                    } catch (InterruptedException ex) {
View Full Code Here

                        }
                    }

                    HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity();
                    String line = EntityUtils.toString(incoming);
                    ContentType contentType = ContentType.getOrDefault(incoming);
                    String charset = contentType.getCharset();
                    if (charset == null) {
                        charset = HTTP.DEFAULT_CONTENT_CHARSET;
                    }
                    RepeatingEntity outgoing = new RepeatingEntity(line, charset, n);
                    outgoing.setChunked(n % 2 == 0);
                    response.setEntity(outgoing);
                } else {
                    throw new HttpException("Invalid request: POST request expected");
                }
            }

        });

        this.server.start();
        DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
        HttpHost host = new HttpHost("localhost", this.server.getPort());

        try {
            for (int i = 0; i < patterns.length; i++) {
                String pattern = patterns[i];
                for (int n = 1000; n < 1020; n++) {
                    if (!conn.isOpen()) {
                        Socket socket = new Socket(host.getHostName(), host.getPort());
                        conn.bind(socket, this.client.getParams());
                    }

                    BasicHttpEntityEnclosingRequest post = new BasicHttpEntityEnclosingRequest(
                            "POST", "/?n=" + n);
                    StringEntity outgoing = new StringEntity(pattern);
                    outgoing.setChunked(n % 2 == 0);
                    post.setEntity(outgoing);

                    HttpResponse response = this.client.execute(post, host, conn);
                    HttpEntity incoming = response.getEntity();
                    Assert.assertNotNull(incoming);
                    InputStream instream = incoming.getContent();
                    ContentType contentType = ContentType.getOrDefault(incoming);
                    String charset = contentType.getCharset();
                    if (charset == null) {
                        charset = HTTP.DEFAULT_CONTENT_CHARSET;
                    }
                    Assert.assertNotNull(instream);
                    BufferedReader reader = new BufferedReader(new InputStreamReader(instream, charset));
View Full Code Here

     * @throws IOException
     *             If there was an exception getting the entity's data.
     */
    public static List <NameValuePair> parse (
            final HttpEntity entity) throws IOException {
        ContentType contentType = ContentType.get(entity);
        if (contentType != null && contentType.getMimeType().equalsIgnoreCase(CONTENT_TYPE)) {
            String content = EntityUtils.toString(entity, Consts.ASCII);
            if (content != null && content.length() > 0) {
                Charset charset = contentType.getCharset();
                if (charset == null) {
                    charset = HTTP.DEF_CONTENT_CHARSET;
                }
                return parse(content, charset);
            }
View Full Code Here

                        }
                    }

                    final HttpEntity incoming = ((HttpEntityEnclosingRequest) request).getEntity();
                    final String line = EntityUtils.toString(incoming);
                    final ContentType contentType = ContentType.getOrDefault(incoming);
                    Charset charset = contentType.getCharset();
                    if (charset == null) {
                        charset = HTTP.DEF_CONTENT_CHARSET;
                    }
                    final RepeatingEntity outgoing = new RepeatingEntity(line, charset, n);
                    outgoing.setChunked(n % 2 == 0);
                    response.setEntity(outgoing);
                } else {
                    throw new HttpException("Invalid request: POST request expected");
                }
            }

        });

        this.server.start();
        final DefaultBHttpClientConnection conn = client.createConnection();
        final HttpHost host = new HttpHost("localhost", this.server.getPort());

        try {
            for (final String pattern : patterns) {
                for (int n = 1000; n < 1020; n++) {
                    if (!conn.isOpen()) {
                        client.connect(host, conn);
                    }

                    final BasicHttpEntityEnclosingRequest post = new BasicHttpEntityEnclosingRequest(
                            "POST", "/?n=" + n);
                    final StringEntity outgoing = new StringEntity(pattern);
                    outgoing.setChunked(n % 2 == 0);
                    post.setEntity(outgoing);

                    final HttpResponse response = this.client.execute(post, host, conn);
                    final HttpEntity incoming = response.getEntity();
                    Assert.assertNotNull(incoming);
                    final InputStream instream = incoming.getContent();
                    final ContentType contentType = ContentType.getOrDefault(incoming);
                    Charset charset = contentType.getCharset();
                    if (charset == null) {
                        charset = HTTP.DEF_CONTENT_CHARSET;
                    }
                    Assert.assertNotNull(instream);
                    final BufferedReader reader = new BufferedReader(new InputStreamReader(instream, charset));
View Full Code Here

    public final synchronized void responseReceived(
            final HttpResponse response) throws IOException, HttpException {
        onResponseReceived(response);
        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            final ContentType contentType = ContentType.getOrDefault(entity);
            onEntityEnclosed(entity, contentType);
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.http.entity.ContentType

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.