Java 中使用 HTTPClient 的 GZip POST 请求

发布于 2024-12-01 09:05:46 字数 132 浏览 2 评论 0原文

我需要向 Web 服务器发送 POST 请求,其中包含压缩的请求参数。我正在使用 Apache HttpClient,并且我读到它支持开箱即用的 Gzip,但我找不到任何有关如何执行我需要的操作的示例。如果有人可以发布一些这方面的例子,我将不胜感激。

I need to send a POST request to a web server which includes a gzipped request parameter. I'm using Apache HttpClient and I've read that it supports Gzip out of the box, but I can't find any examples of how to do what I need. I'd appreciate it if anyone could post some examples of this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阿楠 2024-12-08 09:05:46

您需要首先将该 String 转换为 gzipped byte[] 或(temp)File。假设它不是一个非常大的 String 值,因此 byte[] 对于可用的 JVM 内存来说足够安全:

String foo = "value";
ByteArrayOutputStream baos = new ByteArrayOutputStream();

try (GZIPOutputStream gzos = new GZIPOutputStream(baos)) {
    gzos.write(foo.getBytes("UTF-8"));
}

byte[] fooGzippedBytes = baos.toByteArray();

然后,您可以使用以下命令将其作为多部分主体发送 : HttpClient如下:

MultipartEntity entity = new MultipartEntity();
entity.addPart("foo", new InputStreamBody(new ByteArrayInputStream(fooGzippedBytes), "foo.txt"));

HttpPost post = new HttpPost("http://example.com/some");
post.setEntity(entity);

HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
// ...

注意HttpClient 4.1支持新的ByteArrayBody< /code>可以按如下方式使用:

entity.addPart("foo", new ByteArrayBody(fooGzippedBytes, "foo.txt"));

You need to turn that String into a gzipped byte[] or (temp) File first. Let's assume that it's not an extraordinary large String value so that a byte[] is safe enough for the available JVM memory:

String foo = "value";
ByteArrayOutputStream baos = new ByteArrayOutputStream();

try (GZIPOutputStream gzos = new GZIPOutputStream(baos)) {
    gzos.write(foo.getBytes("UTF-8"));
}

byte[] fooGzippedBytes = baos.toByteArray();

Then, you can send it as a multipart body using HttpClient as follows:

MultipartEntity entity = new MultipartEntity();
entity.addPart("foo", new InputStreamBody(new ByteArrayInputStream(fooGzippedBytes), "foo.txt"));

HttpPost post = new HttpPost("http://example.com/some");
post.setEntity(entity);

HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
// ...

Note that HttpClient 4.1 supports the new ByteArrayBody which can be used as follows:

entity.addPart("foo", new ByteArrayBody(fooGzippedBytes, "foo.txt"));
两相知 2024-12-08 09:05:46

尝试 GzipCompressingEntity 类。如果我要压缩帖子的正文,例如 JSON 对象,我会去:

    // json payload
if (jsonBody != null) {
    
    post.setHeader("Content-Type", "application/json");
    StringEntity requestEntity = new StringEntity( jsonBody, ContentType.APPLICATION_JSON);
    
    if (gzipBody) {
        GzipCompressingEntity gzippedEntity = new GzipCompressingEntity(requestEntity);
        post.setEntity(gzippedEntity);
    }else {
        post.setEntity(requestEntity);
    }
    
}

尚未测试,但我假设要添加参数,您会这样做:

    // add parameters
if (parameters != null && parameters.length > 0){

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    for (int i = 0; i < parameters.length; i++){
        urlParameters.add(new BasicNameValuePair(parameters[i][0], parameters[i][1]));
    }
    post.setEntity(new GzipCompressingEntity(new UrlEncodedFormEntity(urlParameters)));
}

Try the GzipCompressingEntity class. If I'm zipping the body of a post e.g. for a JSON object I would go:

    // json payload
if (jsonBody != null) {
    
    post.setHeader("Content-Type", "application/json");
    StringEntity requestEntity = new StringEntity( jsonBody, ContentType.APPLICATION_JSON);
    
    if (gzipBody) {
        GzipCompressingEntity gzippedEntity = new GzipCompressingEntity(requestEntity);
        post.setEntity(gzippedEntity);
    }else {
        post.setEntity(requestEntity);
    }
    
}

Haven't tested but I assume for adding parameters you'd do:

    // add parameters
if (parameters != null && parameters.length > 0){

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    for (int i = 0; i < parameters.length; i++){
        urlParameters.add(new BasicNameValuePair(parameters[i][0], parameters[i][1]));
    }
    post.setEntity(new GzipCompressingEntity(new UrlEncodedFormEntity(urlParameters)));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文