我正在尝试使用 Multipart 内容类型执行批量导入,但是,无论我尝试什么,我都会不断收到错误 400 - Bad Request。不幸的是,这并没有告诉我太多,批量导入联系人的文档仅说明在验证联系人时出现错误时会发生此错误。这是我目前采用的方法:
public void SendContactFile(string contactListId, FileInfo listFile)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (var content = new MultipartFormDataContent())
{
var contentPieces = new []
{
new KeyValuePair<string, string>("file_name", listFile.Name),
new KeyValuePair<string, string>("lists", contactListId),
};
//var data = new StreamContent(listFile.OpenRead()); //I've tried using both StreamContent and ByteArrayContent; both returned the same failure.
var data = new ByteArrayContent(File.ReadAllBytes(listFile.FullName));
data.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = listFile.Name
};//I've tried with & without this header.
foreach (KeyValuePair<string, string> pair in contentPieces)
{
content.Add(new StringContent(pair.Value), pair.Key);
}
content.Add(data);
var result = client.PostAsync(FullApiAddress, content).Result;
Console.WriteLine("Status Code: {0} - {1}, Request Message: {3}",
result.StatusCode,
result.ReasonPhrase,
result.RequestMessage);
}
}
}
最后,我的 Console.WriteLine() 显示的信息是:
Status Code: BadRequest - Bad Request,
Request Message: Method: POST,
RequestUri: 'https://api.constantcontact.com/v2/activities/addcontacts?api_key=My_API_Key',
Content: System.Net.Http.MultipartFormDataContent,
Headers:
{
Authorization: Bearer My-Access-Token
Accept: application/json
Content-Type: multipart/form-data; boundary="ead4e036-5921-4a98-847b-12h4sgre4
69c"
Content-Length: 556
}
编辑
感谢 Amy 在下面评论中的建议,我使用 Chrome 中的 Postman 扩展程序直接从浏览器执行批量导入。通过这样做,我现在可以排除文件格式可能是我的错误的原因(并删除了这些示例),并且我也知道我正在以正确的方式处理这个问题。因此,我的代码有些地方不太对劲……有人有什么想法吗?
成功的浏览器内请求与上面显示的标头之间的一个区别是它Content-Type: mulitpart/form-data;
具有边界信息,而来自浏览器的信息在每个部分都有该信息。此外,上面的代码没有显示每个部分。Postman 请求预览示例:
Content-Type: multipart/form-data
Cache-Control: no-cache
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="file_name"
ContactList.txt
----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="lists"