Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过http下载音频并将其存储在c中的本地文件夹中#_C#_Http_Audio - Fatal编程技术网

C# 通过http下载音频并将其存储在c中的本地文件夹中#

C# 通过http下载音频并将其存储在c中的本地文件夹中#,c#,http,audio,C#,Http,Audio,有人能给我分享一段c语言的代码吗?我可以使用http请求下载.wmv格式的音频文件并存储在本地文件夹中?你可以使用web客户端 using System.Net; WebClient webClient = new WebClient(); webClient.DownloadFile("http://example.com/myfile.wmv", @"c:\myfile.wmv"); 使用http web请求 HttpWebRequest request = (HttpWebReques

有人能给我分享一段c语言的代码吗?我可以使用http请求下载.wmv格式的音频文件并存储在本地文件夹中?

你可以使用web客户端

using System.Net;

WebClient webClient = new WebClient();
webClient.DownloadFile("http://example.com/myfile.wmv", @"c:\myfile.wmv");
使用http web请求

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://example.com/myfile.wmv");
request.Method = WebRequestMethods.Http.Get;
request.ContentType = "video/x-ms-wmv"; 
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream reader = response.GetResponseStream();

byte[] inBuf = new byte[response.ContentLength];
int bytesToRead = (int)inBuf.Length;
int bytesRead = 0;
while (bytesToRead > 0)
{
    int n = reader.Read(inBuf, bytesRead, bytesToRead);
    if (n == 0)
    break;
    bytesRead += n;
    bytesToRead -= n;
}
FileStream fstr = new FileStream(@"c:\myfile.wmv", FileMode.OpenOrCreate,
                                                     FileAccess.Write);
fstr.Write(inBuf, 0, bytesRead);
reader.Close();
fstr.Close();