400 - 执行批量导入联系人时的错误请求

时间:2015-03-26 22:03:46

标签: c# constantcontact

我尝试使用Multipart内容类型执行批量导入,但是,无论我尝试什么,我都会不断收到错误400 - 错误请求。不幸的是,这并没有告诉我多少,bulk importing of contacts的文档仅指出在验证联系人时出现错误时会发生此错误。这是我目前采用的方法:

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;具有边界信息,而浏览器的信息在每个部分都有该信息。此外,上面的代码并没有显示每个部分。邮差请求预览示例:

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"

1 个答案:

答案 0 :(得分:1)

非常感谢艾米,在这个过程中,他的帮助非常宝贵!

我发现上面使用的大多数代码没有任何错误,除了这些代码:

data.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
    FileName = listFile.Name
};//I've tried with & without this header.

这是我在询问时正在尝试的东西,它打破了请求。我的主要问题是C#,当我为上传的3个部分组成Content-Disposition标头时,不会在名称值周围添加引号。反过来,这又导致返回400 - 错误请求错误。这就是工作代码现在的样子:

using (var content = new MultipartFormDataContent())
{
    var fileNameContent = new StringContent(listFile.Name);
    content.Add(fileNameContent, "\"file_name\"");

    var listContent = new StringContent(contactListId);
    content.Add(listContent, "\"lists\"");

    var dataContent = new ByteArrayContent(File.ReadAllBytes(listFile.FullName));
    content.Add(dataContent, "\"data\"");

    result = client.PostAsync(FullApiAddress, content).Result;
}

注意每个名称值的双引号;这个简单的改变是我需要做的就是让请求工作。我希望这对将来的任何人都有帮助。