7

我正在尝试为 POST 请求创建一个 HTTP 实体,该实体看起来像此处列出的 contextIO API 的一部分:

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

从他们的文档中,他们给出了以下示例:

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--

我尝试使用以下方法执行此操作:

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

但我似乎无法将整个内容包装在 multipart/form-data 部分中。任何想法如何使用 Apache HTTP 客户端创建此请求?

4

2 回答 2

0

尝试添加后:

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);

我还看到他们使用 OAuth 和讨厌的类型(1.0)。计算 OAuth 1.0 签名非常繁琐。您会发现使用 OAuth 库发出请求要容易得多。

于 2013-10-22T04:02:52.190 回答
0

@Chloe 提供的答案没有将 boundary= 参数添加到 content-type 标头。

您可以强制 HTTP 客户端将该参数添加到 Content-Type 标头,但这需要一些技巧。这详细描述了该过程 - https://servicesunavailable.wordpress.com/2015/03/04/using-apache-httpclient-4-x-for-multipart-uploads-with-jersey-1-x-server/

于 2015-03-04T12:21:21.057 回答