Cri*_*riu 5 asp.net-mvc asp.net-web-api
我想尝试这样的POST:
HttpClient hc = new HttpClient();
byte[] bytes = ReadFile(@"my_path");
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("FileName", "001.jpeg"));
postData.Add(new KeyValuePair<string, string>("ConvertToExtension", ".pdf"));
postData.Add(new KeyValuePair<string, string>("Content", Convert.ToBase64String(bytes)));
HttpContent content = new FormUrlEncodedContent(postData);
hc.PostAsync("url", content).ContinueWith((postTask) => {
postTask.Result.EnsureSuccessStatusCode();
});
Run Code Online (Sandbox Code Playgroud)
但我收到这个例外:
无效的URI:Uri字符串太长.
抱怨这条线:HttpContent content = new FormUrlEncodedContent(postData);
.对于小文件它可以工作,但我不明白为什么对于较大的文件它不?
当我发布POST时,内容可能会更大......那么为什么它会抱怨URI?
您应该使用 MultipartFormDataContent ( http://msdn.microsoft.com/en-us/library/system.net.http.multipartformdatacontent%28v=vs.110%29 ) 而不是 FormUrlEncodedContent,它将您的数据作为“application/x -www-form-urlencoded”。
因此,即使您使用 POST 动词,它仍然会 POST 到包含您的数据的很长的 url,因此会出现错误。
内容类型“application/x-www-form-urlencoded”对于发送大量二进制数据或包含非 ASCII 字符的文本是低效的。内容类型“multipart/form-data”应用于提交包含文件、非 ASCII 数据和二进制数据的表单。
见:http : //www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1
有关示例,请查看此答案:ASP.NET WebApi:如何使用 WebApi HttpClient 执行带有文件上传的多部分发布
归档时间: |
|
查看次数: |
3344 次 |
最近记录: |