byte[] buffer; private async void Upload(StorageFile fileStream) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://sv.volcanosoft.com:47581/api/SpeechRecognition?dataType=json")); request.Method = "POST"; request.ContentType = "multipart/form-data"; request.UseDefaultCredentials = true; var file = await fileStream.OpenReadAsync(); byte[] audioDataBytes; audioDataBytes = new byte[file.Size]; for (int i = 0; i < audioDataBytes.Length; i++) { audioDataBytes[i] = (byte)file.AsStreamForRead().ReadByte(); } const int MAX_URI_LENGTH = 32766; string base64audio = System.Convert.ToBase64String(audioDataBytes); StringBuilder sb = new StringBuilder(); for (int i = 0; i < base64audio.Length; i += MAX_URI_LENGTH) { sb.Append(Uri.EscapeDataString(base64audio.Substring(i, Math.Min(MAX_URI_LENGTH, base64audio.Length - i)))); } string uploadRequestString = sb.ToString(); buffer = Encoding.UTF8.GetBytes(uploadRequestString); request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request); } private void ReadCallback(IAsyncResult asynchronousResult) { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; // End the operation. Stream postStream = request.EndGetRequestStream(asynchronousResult); postStream.Write(buffer, 0, buffer.Length); postStream.Close(); request.BeginGetResponse(new AsyncCallback(ResponseCallback), request); } private void ResponseCallback(IAsyncResult asynchronousResult) { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; HttpWebResponse resp = (HttpWebResponse)request.EndGetResponse(asynchronousResult); Stream streamResponse = resp.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse); string responseString = streamRead.ReadToEnd(); Dispatcher.BeginInvoke(new Action(() => { MessageBox.Show(responseString); })); // Close the stream object. streamResponse.Close(); streamRead.Close(); // Release the HttpWebResponse. resp.Close(); }
Invalid 'HttpContent' instance provided. It does not have a 'multipart' content-type header with a 'boundary' parameter. Parameter name: content
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)