1 回答
TA贡献1812条经验 获得超5个赞
我发现了问题:HTTP 客户端的默认超时是 100 秒。源代码在这里:
https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/src/System/Net/Http/HttpClient.cs#L17
我已经像这样更改了我的代码:
public async Task<string> PostFile(string url, string filepath)
{
var request = new HttpClient
{
Timeout = TimeSpan.FromSeconds(3600)
};
var form = new MultipartFormDataContent();
string responseString = null;
using (var fileStream = new FileStream(filepath, mode: FileMode.Open))
{
using (var bufferedStream = new BufferedStream(fileStream))
{
form.Add(new StreamContent(bufferedStream), "file", new FileInfo(filepath).FullName);
var response = await request.PostAsync(url, form);
responseString = await response.Content.ReadAsStringAsync();
fileStream.Close();
}
}
return responseString;
}
问题解决了。
- 1 回答
- 0 关注
- 154 浏览
添加回答
举报