Reputation: 1
I'm developing an application on windows phone 8.1 in c# that can take a picture and send it to a server (in base64) with a post request.
So my problem is that i can't find a method which still work for include my image base64 in the body on my request and send it to my server.
private async void validPicture_Click(object sender, RoutedEventArgs e)
{
Encode("ms-appx:///xx/xxx.jpg");
try
{
var client = new Windows.Web.Http.HttpClient();
var uri = new Uri("http://x.x.x.x:x/picture/");
string pathBase64 = Encode("ms-appx:///xx/xxx.jpg").ToString();
Dictionary<string, string> pairs = new Dictionary<string, string>();
pairs.Add("type", "recognition");
HttpFormUrlEncodedContent formContent = new HttpFormUrlEncodedContent(pairs);
Windows.Web.Http.HttpResponseMessage response = await client.PostAsync(uri, formContent);
string content = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
}
}
catch (Exception eu)
{
}
}
If you are a question or need more information please tell me.
Thanks for your time.
Upvotes: 0
Views: 608
Reputation: 763
First of all you have to read image file from storage and convert bytes to base64 string.
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(PHOTO_PATH));
byte[] rawBytes;
using (Stream stream = await file.OpenStreamForReadAsync())
{
rawBytes = new byte[stream.Length];
await stream.ReadAsync(rawBytes, 0, rawBytes.Length);
}
string base64Content = Convert.ToBase64String(rawBytes);
Then you have to make request with that content. I'm not sure how yours server accept requests but here is example of sending request with that string in content.
var httpClient = new Windows.Web.Http.HttpClient();
IBuffer content = CryptographicBuffer.ConvertStringToBinary(base64Content, BinaryStringEncoding.Utf8);
var request = new HttpBufferContent(content);
HttpResponseMessage response = await httpClient.PostAsync(new Uri(SERVER_URL), request);
Upvotes: 1