Aymeeeric
Posts: 13
Joined: Mon Feb 29, 2016 8:30 am

[RESOLVED] Html form POST

Mon Jul 04, 2016 7:16 am

Hi !

I have to make an automatic post to a web form with my IoT application.
It's a simple web form (only few parameters) but one of them is a file (audio)...

How to do this ? Is it possible with the HttpClient object ?

Hope you can help me...

Thanks in advance !

Image
Last edited by Aymeeeric on Mon Jul 04, 2016 8:46 am, edited 1 time in total.

Aymeeeric
Posts: 13
Joined: Mon Feb 29, 2016 8:30 am

Re: Html form POST

Mon Jul 04, 2016 8:42 am

Ok, so after many tries, here is the solution :

Code: Select all

 // Upload :
using (var client = new HttpClient())
{
	using (var content = new MultipartFormDataContent())
	{
		var values = new[]
		{
			// Form data :
			new KeyValuePair<string, string>("audio[audio_id]",     "DB_"),
			new KeyValuePair<string, string>("audio[type_report]",  "TEST"),
		};

		foreach (var keyValuePair in values)
		{
			content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
		}

		// File (form-data too) :
		var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(file.Path));
		fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
		{
			Name = "audio[audioURL]",
			FileName = fileName
		};
		content.Add(fileContent);
		

		var requestUri = @"/global.php/audio/create";
		var response = client.PostAsync(requestUri, content).Result;
		var contents = response.Content.ReadAsStringAsync();
	}
}

Return to “Windows 10 for IoT”