我试图上传一个文件通过一个网站API在我的C# Windows 8.1应用程序。我想使用Windows.Web.Http库。我可以选择一个带有FilePicker (fileChoosenFromFilePicker var)的文件。
我每次都会收到一个http 400 (坏请求)错误。
问题有人能帮我吗?如何看到由代码生成的POST请求?我可以在服务器端进行数据包捕获,但是我想在客户端看到。
码
在API文档中,有一个例子:
curl -H "Authorization: Token f2210dacd9c6ccb8133606d94ff8e61d99b477fd" -F file=@test.txt -F filename=test.txt -F parent_dir=/ http://cloud.seafile.com:8082/upload-api/ef881b22这是我的密码:
var HttpClientUpload = new HttpClient();
HttpMultipartContent requestUpload = new HttpMultipartContent();
HttpMultipartFormDataContent requestUploadContent = new HttpMultipartFormDataContent();
var fileContent = await fileChoosenFromFilePicker.OpenReadAsync();
HttpFormUrlEncodedContent requestUploadData = new HttpFormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("filename", fileChoosenFromFilePicker.Name),
new KeyValuePair<string, string>("parent_dir", "/")
});
HttpClientUpload.DefaultRequestHeaders.Add("Authorization", "token " + authorization);
IInputStream inputStream = await fileChoosenFromFilePicker.OpenAsync(FileAccessMode.Read);
requestUploadContent.Add(new HttpStreamContent(inputStream), "myFile", fi.Name);
try
{
HttpResponseMessage response = await HttpClientUpload.PostAsync(uristringForUpload, requestUpload);
response.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
}编辑:
我用了你的密码。并做了一些改变:
var HttpClientUpload = new HttpClient(filter);
HttpMultipartFormDataContent requestUploadContent = new HttpMultipartFormDataContent();
var fileContent = await fileChoosenFromFilePicker.OpenReadAsync();
HttpClientUpload.DefaultRequestHeaders.Add("Authorization", "token " + authorization);
FileInfo fi = new FileInfo(fileChoosenFromFilePicker.Path);
string fileName = fi.Name;
var inputStream = await fileChoosenFromFilePicker.OpenAsync(FileAccessMode.Read);
requestUploadContent.Add(new HttpStreamContent(inputStream), "myFile", fi.Name);
var values = new[]
{
new KeyValuePair<string, string>("filename", fileName),
new KeyValuePair<string, string>("parent_dir", "/")
};
foreach (var keyValuePair in values)
{
requestUploadContent.Add(new HttpStringContent(keyValuePair.Value), keyValuePair.Key);
}
try
{
HttpResponseMessage responseLogin = await HttpClientUpload.PostAsync(uristringForUpload, requestUploadContent);
responseLogin.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
}现在好多了。但是它还是不起作用。
在这里您可以找到POST结果:http://requestb.in/p5jvlfp5?inspect#13hh5o
如果我用卷曲做的话,我的结果是:http://requestb.in/pwng8npw?inspect
在我的版本中,在POST结果中,没有“内容-类型:应用程序/八进制流”。我怎么才能具体说明呢?或者是因为HttpStreamContent的问题?
发布于 2015-01-30 17:28:44
据我所知,你们没有把这些信息组合在一起。当您到达实际的PostAsync()调用时,您将发送一个类型为HttpMultipartContent的完全空包。您对变量requestUpload进行维度化,然后除了post之外,永远不会对它做任何事情。
尝试通过创建外部HttpMultipartFormDataContent对象来组合请求,您将向其中添加HttpStreamContent和HttpFormUrlEncodedContent对象,如下所示。我试图在Seafile的python上载方法文档的请求中尽可能地重新创建服务器。
var client = new HttpClient();
var request = new HttpMultipartFormDataContent();
request.Headers.Add("Authorization", "Token " + authorization);
// Add file content request part
var fileContent = await fileChosenFromFilePicker.OpenAsync(FileAccessMode.Read);
var requestContent = new HttpStreamContent(fileContent);
requestContent.Headers.Add("Content-Type", "application/octet-stream");
request.Add(requestContent, "file", fileChosenFromFilePicker.Name);
// Add form data request part
request.Add(new HttpFormUrlEncodedContent(new Dictionary<string, string>
{
{"parent_dir", "/"}
}));
try
{
// Submit multipart request object
var response = await client.PostAsync(new Uri(uriStringForUpload), request);
response.EnsureSuccessStatusCode();
}
catch (Exception ex)
{
// TODO
} https://stackoverflow.com/questions/28238499
复制相似问题