Java Code Examples for org.apache.http.entity.BasicHttpEntity#setContentLength()

The following examples show how to use org.apache.http.entity.BasicHttpEntity#setContentLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpUrlConnStack.java    From simple_net_framework with MIT License 6 votes vote down vote up
/**
 * 执行HTTP请求之后获取到其数据流,即返回请求结果的流
 * 
 * @param connection
 * @return
 */
private HttpEntity entityFromURLConnwction(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream = null;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException e) {
        e.printStackTrace();
        inputStream = connection.getErrorStream();
    }

    // TODO : GZIP 
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());

    return entity;
}
 
Example 2
Source File: HurlStack.java    From volley with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    
    return entity;
}
 
Example 3
Source File: RequestExecutor.java    From vertx-deploy-tools with Apache License 2.0 6 votes vote down vote up
HttpPost createPost(DeployRequest deployRequest, String host) {
    URI deployUri = createDeployUri(host, deployRequest.getEndpoint());
    log.info("Deploying to host : " + deployUri.toString());
    HttpPost post = new HttpPost(deployUri);
    if (!StringUtils.isNullOrEmpty(authToken)) {
        log.info("Adding authToken to request header.");
        post.addHeader("authToken", authToken);
    }

    ByteArrayInputStream bos = new ByteArrayInputStream(deployRequest.toJson(false).getBytes());
    BasicHttpEntity entity = new BasicHttpEntity();
    entity.setContent(bos);
    entity.setContentLength(deployRequest.toJson(false).getBytes().length);
    post.setEntity(entity);
    return post;
}
 
Example 4
Source File: ExtHttpClientStack.java    From android_volley_examples with Apache License 2.0 6 votes vote down vote up
private org.apache.http.HttpEntity convertEntityNewToOld(HttpEntity ent) 
        throws IllegalStateException, IOException {
    
    BasicHttpEntity ret = new BasicHttpEntity();
    if (ent != null) {
        ret.setContent(ent.getContent());
        ret.setContentLength(ent.getContentLength());
        Header h;
        h = ent.getContentEncoding();
        if (h != null) {
            ret.setContentEncoding(convertheaderNewToOld(h));
        }
        h = ent.getContentType();
        if (h != null) {
            ret.setContentType(convertheaderNewToOld(h));
        }
    }

    return ret;
}
 
Example 5
Source File: HttpKnife.java    From WeGit with Apache License 2.0 6 votes vote down vote up
/**
 * 获取响应报文实体
 * 
 * @return
 * @throws IOException
 */
private HttpEntity entityFromConnection() throws IOException {
	BasicHttpEntity entity = new BasicHttpEntity();
	InputStream inputStream;
	try {
		inputStream = connection.getInputStream();
	} catch (IOException ioe) {
		inputStream = connection.getErrorStream();
	}
	if (GZIP.equals(getResponseheader(ResponseHeader.HEADER_CONTENT_ENCODING))) {
		entity.setContent(new GZIPInputStream(inputStream));
	} else {
		entity.setContent(inputStream);
	}
	entity.setContentLength(connection.getContentLength());
	entity.setContentEncoding(connection.getContentEncoding());
	entity.setContentType(connection.getContentType());
	return entity;
}
 
Example 6
Source File: HurlStack.java    From SaveVolley with Apache License 2.0 6 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 *
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
/*
 *  通过一个 HttpURLConnection 获取其对应的 HttpEntity ( 这里就 HttpEntity 而言,耦合了 Apache )
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    // 设置 HttpEntity 的内容
    entity.setContent(inputStream);
    // 设置 HttpEntity 的长度
    entity.setContentLength(connection.getContentLength());
    // 设置 HttpEntity 的编码
    entity.setContentEncoding(connection.getContentEncoding());
    // 设置 HttpEntity Content-Type
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 7
Source File: HttpPostGet.java    From ApiManager with GNU Affero General Public License v3.0 6 votes vote down vote up
public static String postBody(String url, String body, Map<String, String> headers, int timeout) throws Exception {
    HttpClient client = buildHttpClient(url);
    HttpPost httppost = new HttpPost(url);
    httppost.setHeader("charset", "utf-8");

    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout)
            .setConnectionRequestTimeout(timeout).setStaleConnectionCheckEnabled(true).build();
    httppost.setConfig(requestConfig);
    buildHeader(headers, httppost);

    BasicHttpEntity requestBody = new BasicHttpEntity();
    requestBody.setContent(new ByteArrayInputStream(body.getBytes("UTF-8")));
    requestBody.setContentLength(body.getBytes("UTF-8").length);
    httppost.setEntity(requestBody);
    // 执行客户端请求
    HttpResponse response = client.execute(httppost);
    HttpEntity entity = response.getEntity();
    return EntityUtils.toString(entity, "UTF-8");
}
 
Example 8
Source File: ClusterServiceServletHttpHandler.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
private void writeResponse(HttpResponse response, int statusCode, String content) {
    if (content == null) {
        content = "";
    }
    response.setStatusCode(statusCode);
    final BasicHttpEntity body = new BasicHttpEntity();
    body.setContentType("text/html; charset=UTF-8");

    final byte[] bodyData = content.getBytes();
    body.setContent(new ByteArrayInputStream(bodyData));
    body.setContentLength(bodyData.length);
    response.setEntity(body);
}
 
Example 9
Source File: HurlStack.java    From AndroidProjects with MIT License 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 *
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 10
Source File: HurlStack.java    From device-database with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 11
Source File: OkHttpStack.java    From openshop.io-android with MIT License 5 votes vote down vote up
private static HttpEntity entityFromOkHttpResponse(Response r) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = r.body();

    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(r.header("Content-Encoding"));

    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }
    return entity;
}
 
Example 12
Source File: HurlStack.java    From jus with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 13
Source File: OkHttpStack.java    From wasp with Apache License 2.0 5 votes vote down vote up
private static HttpEntity entityFromOkHttpResponse(Response response) throws IOException {
  BasicHttpEntity entity = new BasicHttpEntity();
  ResponseBody body = response.body();

  entity.setContent(body.byteStream());
  entity.setContentLength(body.contentLength());
  entity.setContentEncoding(response.header("Content-Encoding"));

  if (body.contentType() != null) {
    entity.setContentType(body.contentType().type());
  }
  return entity;
}
 
Example 14
Source File: OkHttp3Stack.java    From QuickLyric with GNU General Public License v3.0 5 votes vote down vote up
private static HttpEntity getEntity(Response response) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = response.body();
    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(response.header("Content-Encoding"));
    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }
    return entity;
}
 
Example 15
Source File: HurlStack.java    From SimplifyReader with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link org.apache.http.HttpEntity} from the given {@link java.net.HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 16
Source File: HurlStack.java    From pearl with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 17
Source File: OkHttpStack.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
private static HttpEntity entityFromOkHttpResponse(com.squareup.okhttp.Response r) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = r.body();

    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(r.header("Content-Encoding"));

    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }
    return entity;
}
 
Example 18
Source File: OkHttpStack.java    From android with MIT License 5 votes vote down vote up
private static HttpEntity getEntity(Response response) throws IOException {
    BasicHttpEntity entity = new BasicHttpEntity();
    ResponseBody body = response.body();
    entity.setContent(body.byteStream());
    entity.setContentLength(body.contentLength());
    entity.setContentEncoding(response.header("Content-Encoding"));
    if (body.contentType() != null) {
        entity.setContentType(body.contentType().type());
    }
    return entity;
}
 
Example 19
Source File: HurlStack.java    From WayHoo with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}
 
Example 20
Source File: HurlStack.java    From android_tv_metro with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes an {@link HttpEntity} from the given {@link HttpURLConnection}.
 * @param connection
 * @return an HttpEntity populated with data from <code>connection</code>.
 */
private static HttpEntity entityFromConnection(HttpURLConnection connection) {
    BasicHttpEntity entity = new BasicHttpEntity();
    InputStream inputStream;
    try {
        inputStream = connection.getInputStream();
    } catch (IOException ioe) {
        inputStream = connection.getErrorStream();
    }
    entity.setContent(inputStream);
    entity.setContentLength(connection.getContentLength());
    entity.setContentEncoding(connection.getContentEncoding());
    entity.setContentType(connection.getContentType());
    return entity;
}