将来自Web请求的响应保存为mp3文件

时间:2013-11-29 22:16:47

标签: c#

当我尝试读取流时,我得到超时异常。我从服务器得到的响应是mp3文件,我不知道如何将其保存为mp3。

            WebClient webClient = new WebClient();
            int selectedIndex = listBox2.SelectedIndex;

            JObject o = JObject.Parse(result);
            string SongID = o["SongID"].ToString();
            string SongName = o["SongName"].ToString();
            string Year = o["Year"].ToString();
            string AlbumName = o["AlbumName"].ToString();

            textBox4.Text = SongID + Environment.NewLine + SongName + Environment.NewLine + Year + Environment.NewLine + AlbumName;
           string stream = Webpost("{\"header\":{\"token\":\"" +prepToken("getStreamKeyFromSongIDEx", ":chickenFingers:")+"\",\"privacy\":0,\"country\":{\"DMA\":0,\"CC1\":0,\"IPR\":0,\"CC2\":0,\"CC3\":2305843009213694000,\"ID\":190,\"CC4\":0},\"uuid\":\"8E5D1ABD-EE1B-4498-B960-8E46077E8ED4\",\"client\":\"jsqueue\",\"session\":\""+session+"\",\"clientRevision\":\"20130520\"},\"method\":\"getStreamKeyFromSongIDEx\",\"parameters\":{\"mobile\":false,\"prefetch\":false,\"songID\":"+SongID+",\"type\":0,\"country\":{\"DMA\":0,\"CC1\":0,\"IPR\":0,\"CC2\":0,\"CC3\":2305843009213694000,\"ID\":190,\"CC4\":0}}}", "http://*****.com/more.php?getStreamKeyFromSongIDEx", "POST", "text/plain");
           JObject ss = JObject.Parse(stream);
            string streamkey = ss["result"]["streamKey"].ToString();
            string ip = ss["result"]["ip"].ToString(); 
            textBox4.Text += Environment.NewLine+ "downloiad url: " + ip + Environment.NewLine + "stream key: " + streamkey;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + ip + "/stream.php");              
            request.Method = "POST";
            string postData = "streamKey="+ streamkey;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentLength = postData.Length;   
            request.Host = ip;
            request.Accept =  "*/*";
            request.Headers.Add("Origin", string.Format("http://*********.com"));
            request.Headers.Add("Accept-Language", string.Format("sv-SE,sv;q=0.8,en-US;q=0.6,en;q=0.4"));
            request.Headers.Add("Accept-Encoding", string.Format("gzip,deflate,sdch"));
            request.Headers.Add("Cookie", string.Format("ismobile=no; PHPSESSID="+ session + "__utma=111479378.517776767.1385046829.1385170025.1385750338.12; __utmb=111479378.14.9.1385751143490; __utmc=111479378; __utmz=111479378.1385157436.6.2.utmcsr=nettech.wikia.com|utmccn=(referral)|utmcmd=referral|utmcct=/wiki/*****_Internal_API"));
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36";
            textBox4.Text += Environment.NewLine + "headers: " + Environment.NewLine + request.Headers;
            textBox4.Text += Environment.NewLine + "StreamKey:" + byteArray;
            Stream respStream = request.GetRequestStream();

1 个答案:

答案 0 :(得分:2)

你可以尝试这样的事情

   HttpWebRequest request = (HttpWebRequest)WebRequest.Create("mp3 url");          
         HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        // Get the stream associated with the response.
        Stream receiveStream = response.GetResponseStream();
        byte[] buffer = new byte[32768];
        using (FileStream fileStream = File.Create("yourfullnamepath.mp3"))
        {
            while (true)
            {
                int read = receiveStream.Read(buffer, 0, buffer.Length);
                if (read <= 0)
                    break;
                fileStream.Write(buffer, 0, read);
            }
         }