简体   繁体   English

使用Apache HTTP客户端创建multipart / form-data实体

[英]Creating a multipart/form-data entity with Apache HTTP client

I'm trying to create an HTTP entity for a POST request that looks like the one listed as part of the contextIO API listed here: 我正在尝试为POST请求创建一个HTTP实体,该请求类似于此处列出的contextIO API的一部分:

http://context.io/docs/2.0/accounts/messages#post http://context.io/docs/2.0/accounts/messages#post

From their docs, they give the following example: 从他们的文档中,他们给出了以下示例:

POST /2.0/accounts/4f01234567890abcdef09876/messages/ HTTP/1.1
Host: api.context.io
Accept: */*
Authorization: OAuth oauth_consumer_key="abcdef1234",oauth_version="1.0",oauth_timestamp="1327695986",oauth_nonce="6dPrHNDrx5hzfHkn",oauth_signature_method="HMAC-SHA1",oauth_signature="MFOyvf5Ykcsn7une48kGW0Aharw%3D"
Content-Type: multipart/form-data; boundary=------someRandomBoundary
Content-Length: 1917

--------someRandomBoundary
Content-Disposition: form-data; name="dst_folder"

testFolder
--------someRandomBoundary
Content-Disposition: form-data; name="message"; filename="message.eml"
Content-Type: message/rfc822

Delivered-To: jim@bob.com
Received: by 101.42.118.54 with SMTP id hp10cs194007icb;
        Thu, 13 Jan 2012 15:02:20 -0700 (PDT)
Return-Path: <blahblahblah@someisp.com>
Received: from [192.168.1.5] (71-211-196-225.hlrn.bob.com. [71.211.196.225])
        by mx.bob.com with ESMTPS id u41si888834ybu.20.2011.10.13.14.54.54
        (version=TLSv1/SSLv3 cipher=OTHER);
        Thu, 13 Jan 2012 15:02:20 -0700 (PDT)
Received: from mail-gx0-f174.someisp.com (mail-gx0-f174.someisp.com [109.45.123.134])
        by mx.someisp.com with ESMTPS id u41si888834ybu.20.2011.10.13.14.54.54
        (version=TLSv1/SSLv3 cipher=OTHER);
        Thu, 13 Jan 2012 14:54:53 -0700 (PDT)
Message-ID: <2E973E2A.3150305@bob.com>
Date: Thu, 13 Jan 2012 15:54:50 -0600
From: Dave Davidson <blahblahblah@someisp.com>
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.21) Gecko/20110831 Thunderbird/3.1.13
MIME-Version: 1.0
To: Jim Bob <jim@bob.com>
Subject: Just sending out a test message
Content-Type: multipart/alternative;
 boundary="------------030009050309030308020807"

This is a multi-part message in MIME format.
--------------030009050309030308020807
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit

Yo! This is a test multi-part message.


--------------030009050309030308020807
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#ffff00" text="#ff0000">
    Yo! This is a test multi-part message.<br>
  </body>
</html>

--------------030009050309030308020807--

--------someRandomBoundary--

I've tried to do this using: 我试过这样做:

HttpEntity entity = MultipartEntityBuilder.create()
                    .addBinaryBody("message", messageSource.getBytes(), ContentType.create("message/rfc822"), "message.eml")
                    .addTextBody("dst_folder", label)
                    .addTextBody("dst_source", "0")
                    .build();

But I can't seem to wrap the whole thing in a multipart/form-data section. 但我似乎无法将整个事件包装在multipart / form-data部分中。 Any ideas how to create this request using the Apache HTTP Client? 有关如何使用Apache HTTP Client创建此请求的任何想法?

Try adding this after: 尝试在以下后添加:

HttpPost httpPost = new HttpPost("http://api.context.io//2.0/accounts/4f01234567890abcdef09876/messages/");
httpPost.setEntity(entity);
HttpClient httpClient = new DefaultHttpClient();  
HttpResponse response = httpClient.execute(httpPost);

I also see they use OAuth, and the nasty kind (1.0). 我也看到他们使用OAuth和讨厌的那种(1.0)。 Calculating an OAuth 1.0 signature is very tedious. 计算OAuth 1.0签名非常繁琐。 You will find it much easier to use an OAuth library to make the requests. 您会发现使用OAuth库来发出请求要容易得多。

The answer provided by @Chloe does not add the boundary= parameter to the content-type header. @Chloe提供的答案不会将boundary =参数添加到content-type标头中。

You can force HTTP Client to add that parameter to Content-Type header, but it takes some hacks. 您可以强制HTTP客户端将该参数添加到Content-Type标头,但这需要一些黑客攻击。 This describes the process in detail - https://servicesunavailable.wordpress.com/2015/03/04/using-apache-httpclient-4-x-for-multipart-uploads-with-jersey-1-x-server/ 这将详细描述该过程 - https://servicesunavailable.wordpress.com/2015/03/04/using-apache-httpclient-4-x-for-multipart-uploads-with-jersey-1-x-server/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM