繁体   English   中英

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

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

我正在尝试为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 Client创建此请求的任何想法?

尝试在以下后添加:

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库来发出请求要容易得多。

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

暂无
暂无

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

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