如何POST音频文件并获得响应WP8 C# [英] How can POST audio file and get response WP8 C#

查看:120
本文介绍了如何POST音频文件并获得响应WP8 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我正在尝试将音频文件发布到我的Web服务(ASP.Net 4.5 Web API)并从服务器获取响应但是有错误。所以请帮我解决这个问题



这里我的代码为POST音频



Hi. i'm trying to POST audio file to my Web Service ( ASP.Net 4.5 Web API ) and get Response from server but have error. So please help me fix that

Here my code to POST Audio

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();
        }





这里我在服务器上的错误





And here my error on Server

Invalid 'HttpContent' instance provided. It does not have a 'multipart' content-type header with a 'boundary' parameter.
Parameter name: content

推荐答案

问题出在你的服务器端代码中,你没有发帖,所以拿一个看一下这篇文章,看看你是否能弄清楚你的服务器端代码有什么问题。

http://stackoverflow.com/questions/7460088/reading-file-input-from-a-multipart-form-data-post [<一个href =http://stackoverflow.com/questions/7460088/reading-file-input-from-a-mpartpart-form-data-posttarget =_ blanktitle =New Window> ^ ]
The problem is in your server side code, you didn't post that so take a look at this post and see if you can figure out what is wrong with your server side code.
http://stackoverflow.com/questions/7460088/reading-file-input-from-a-multipart-form-data-post[^]


它仅在WP8中存在问题,但在W8和桌面应用程序版本上运行良好:(
It have problem in WP8 only, but on W8 and Desktop App version working very well :(


这篇关于如何POST音频文件并获得响应WP8 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆