尝试通过 Box API /w C# returns 错误 500 上传文件,但我可以使用 cURL 上传相同的文件。我的 C# 代码有什么问题?

Trying to upload a file via Box API /w C# returns error 500, but I can upload the same file with cURL just fine. What is wrong with my C# code?

我正在尝试使用 Box API 上传文件。我已经在 cURL 中完成了示例,

curl https://upload.box.com/api/2.0/files/content -k -H
 "Authorization: Bearer (access token)" -F filename=@"File path and name" -F folder_id=3015577881

我可以上传我的文件,并在我将其定向到的文件夹中看到它。我希望能够从 C# 控制台应用程序执行此操作。

var url = "https://upload.box.com/api/2.0/files/content";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "multipart/form-data";
req.Headers.Add("Authorization", string.Format("Bearer {0}", accessToken));
var outgoingQueryString = HttpUtility.ParseQueryString(String.Empty);
outgoingQueryString.Add("filename", String.Format("@{0}",fileName));
outgoingQueryString.Add("folder_id", folderId);
string postData = outgoingQueryString.ToString();
var ascii = new ASCIIEncoding();
byte[] postBytes = ascii.GetBytes(postData);
var postStream = req.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Flush();
postStream.Close();
req.GetResponse();

有了这个,我得到了错误 500。当我尝试在没有内容类型的情况下执行它时,我得到了错误 405 (method_not_allowed)。我试过其他内容类型,他们也给了我 405。我做错了什么?

使用 Box .Net 客户端,您可以像这样上传新文件:

BoxFileRequest req = new BoxFileRequest()
{
    Name = "NewFile",
    Parent = new BoxRequestEntity() { Id = "0" }
};

using (var stream = System.IO.File.Open(@"c:\path\to\file.txt", FileMode.Open))
{
    BoxFile f = await client.FilesManager.UploadAsync(request, stream);                
}