我写了一个简单的控制台应用程序来读取文件并通过api上传到d365。这些文件是PDF。
当我用文件流读取pdf文件时,我仍然会遇到一个错误:
ReadTimeout = 'filestream.ReadTimeout'
正在投掷
'System.InvalidOperationException'
.
经过几个小时的研究,我不知道为什么我不能阅读PDF文件。
这是我的源代码:
using (var filestream = File.OpenRead($"C:\\test\\test.pdf"))
{
var length = filestream.Length.ToString();
var streamContent = new StreamContent(filestream);
streamContent.Headers.Add("Content-Type", "application/octet-stream");
streamContent.Headers.Add("Content-Length", length);
var _application = ConfidentialClientApplicationBuilder
.Create(appid)
.WithClientSecret(clientsecret)
.WithAuthority(new Uri($"https://login.microsoftonline.com/aadid"))
.Build();
var authResult = _application.AcquireTokenForClient(new[] { $"orgname.crm4.dynamics.com/.default" })
.ExecuteAsync().ConfigureAwait(true);
var temp = Task.Run(async () => { return await authResult; });
temp.Wait();
var token = temp.Result;
using (var req = new HttpRequestMessage(new HttpMethod("PATCH"), url))
{
req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);
req.Content = streamContent;
//req.Content.Headers.Add("Content-Type", "application/octet-stream");
req.Content.Headers.Add("x-ms-file-name", nameCleared + ".pdf");
HttpClient Client = new HttpClient();
using (var response = Client.SendAsync(req))
{
response.Wait();
response.Result.EnsureSuccessStatusCode();
}
}
}