gpt4 book ai didi

java - 将 Content-ID 添加到多部分实体

转载 作者:行者123 更新时间:2023-11-29 03:38:23 25 4
gpt4 key购买 nike

鉴于我们提出了多部分请求。我们现在需要添加一个 content-id。下面是我们试图用来创建多部分请求的代码:

MultipartEntity mpEntity = new MultipartEntity();
StringBody body;
try
{
body = new StringBody( xml, "application/xml", Charset.forName( "UTF-8" ) );
byte[] data = getBytesFromFile( image );
ByteArrayBody bab = new ByteArrayBody( data, "image/jpeg", "test_image_cid" );
mpEntity.addPart( "body", body );
mpEntity.addPart( "test_image_cid", bab );

} catch ( UnsupportedEncodingException e )
{
e.printStackTrace();
}

HttpPost request = new HttpPost("http://10.1.1.1");
request.addHeader( "Authorization", authorization_header_values );
request.addHeader( "Content-Type", "Multipart/Related" );
request.setEntity( mpEntity );
return request;

这是我们调用的网络服务请求的内容:

<?xml version="1.0" encoding="utf-8"?> <request method="receipt.create"> 
<receipt>
<expense_id>1</expense_id> <!-- id of expense -->
<image>cid:xxxxxxxxxxxxx</image> <!-- content-id used on the related binary content -->
</receipt>
</request>

这是我们从服务器返回的用于调试的内容:

POST/HTTP/1.1授权:OAuth realm="", oauth_version="1.0", oauth_consumer_key="key", oauth_token="token", oauth_timestamp="1358197676614", oauth_nonce="1111111", oauth_signature_method="PLAINTEXT", oauth_signature="signature"内容类型:多部分/相关用户代理:代理内容长度:2336363主机:10.1.1.1连接:保持 Activity 状态

--HPeiFlrswQmM8Mi1uoWpzJRfrnp3AMtZjpCdtContent-Disposition:表单数据;名称=“正文”内容类型:应用程序/xml;字符集=UTF-8内容传输编码:8bit

<?xml version='1.0' encoding='UTF-8' ?>
<request method="receipt.create">
<receipt>
<expense_id>979</expense_id>
<image>cid:test_image_cid</image>
</receipt>
</request>

--HPeiFlrswQmM8Mi1uoWpzJRfrnp3AMtZjpCdtContent-Disposition:表单数据;名称=“test_image_cid”;文件名="test_image_cid"内容类型:图像/jpeg内容传输编码:二进制

我们一直在研究如何将 Content-ID 添加到此请求中。这个电话中有什么明显的遗漏吗?还有另一种方法来构建此请求吗?感谢您的任何建议!

最佳答案

要添加 Content-Id 或与此相关的任何其他字段,您必须使用 FormBodyPart。简单地说,拆分这些行:

ByteArrayBody bab = new ByteArrayBody( data, "image/jpeg", "test_image_cid" );
mpEntity.addPart( "body", body );

进入这些行:

ByteArrayBody bab = new ByteArrayBody( data, "image/png", "byte_array_image" );
FormBodyPart fbp = new FormBodyPart( "form_body_name", bab );
fbp.addField( "Content-Id", "ID_GOES_HERE" );
mpEntity.addPart( fbp );

这应该为您完成!

关于java - 将 Content-ID 添加到多部分实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14327131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com