I'm trying to upload a data over network using HttpClient / HttpContent However I can't seem to find a proper way to send a file this way.
Here is my current code:
private async Task<APIResponse> MakePostRequest(string RequestUrl, string Content)
{
HttpClient httpClient = new HttpClient();
HttpContent httpContent = new StringContent(Content);
APIResponse serverReply = new APIResponse();
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
try {
Console.WriteLine("Sending Request: " + RequestUrl + Content);
HttpResponseMessage response = await httpClient.PostAsync(RequestUrl, httpContent).ConfigureAwait(false);
}
catch (HttpRequestException hre)
{
Console.WriteLine("hre.Message");
}
return (serverReply);
}
Content is a string of that form: paramname=value¶m2name=value¶m3name=value.. Point is that I have to actually send a file (photo) over this request.
It seems to work fine for each parameters but the file itself (I have to send two authentification keys in the post request, and they are recognized)
I proceed this way to retreive the picture as a string which might be one of the main reason it fails ? :/
byte[] PictureData = File.ReadAllBytes(good_path);
string encoded = Convert.ToBase64String(PictureData);
Am I doing anything wrong ? Is there another and better way to create a proper POST request (it has to be async and support file upload)
Thanks.