My C# app uploads file to some API, I'm using multipart request, i.e I'm uploading a json string and binary contect of the file, it works fine for most files, but for very few it does nothing, I mean let's try for file named file.pdf
:
My code looks roughly as follows:
public async Task<Dictionary<string , string>> Upload(string filePath)
{
FileInfo fi = new FileInfo(FilePath);
string jsonString="some json string";
byte[] fileContents=File.ReadAllBytes(fi.FullName);
Uri webService = new Uri(url);
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post , webService);
requestMessage.Method = HttpMethod.Post;
requestMessage.Headers.Add("Authorization" , "MyKey1234");
const string boundry = "------------------My-Boundary";
MultipartFormDataContent multiPartContent = new MultipartFormDataContent(boundry);
ByteArrayContent byteArrayContent = new ByteArrayContent(fileContents);
multiPartContent.Add(byteArrayContent);
requestMessage.Content = multiPartContent;
HttpClient httpClient = new HttpClient();
Console.WriteLine("before");
HttpResponseMessage httpResponse = await httpClient.SendAsync(requestMessage , HttpCompletionOption.ResponseContentRead , CancellationToken.None);
Console.WriteLine("after");
}
The caller:
myDictionary = await Upload(filePath);
Output:
before
Press any key to continue . . .
I mean there is no exception, nothing, what is this? a bug?
Edit
The structure of the console app is as follows:
class Program
{
static void Main(string[] args)
{
new MyClass().Start();
}
}
And inside MyClass:
public async void Start()
{
myDictionary = await Upload(filePath);
}