从 VB.NET 中的 URL 保存 MP3 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18177624/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:37:01  来源:igfitidea点击:

Save MP3 file from URL in VB.NET

vb.netdownloadmp3

提问by Anonymous

I'm attempting to download an .mp3 file from my server, and save it locally, in VB.NET. However, when I do so, the saved file is corrupt, as the contents contain multiple lines of things like below, with a ton of random/weird characters below them (I'm assuming the building blocks of the audio file).

我正在尝试从我的服务器下载一个 .mp3 文件,并将其保存在本地的 VB.NET 中。但是,当我这样做时,保存的文件已损坏,因为内容包含多行如下内容,下面有大量随机/奇怪的字符(我假设是音频文件的构建块)。

Here's what I am seeing: http://i.troll.ws/11d37d00.png

这是我所看到的:http: //i.troll.ws/11d37d00.png

date = "20130811_18:22:58.466";
host = "DC3APS421";
kbps = "48";
khz = "22050";

When I attempt to play the .mp3 file in WMP, it also says it's corrupt. This is the code I'm using to download and save it.

当我尝试在 WMP 中播放 .mp3 文件时,它也说它已损坏。这是我用来下载和保存它的代码。

' Download the MP3 file contents

Dim request As WebRequest = WebRequest.Create("http://www.mysite.org/secure/speech/?text=Test.")
request.Credentials = CredentialCache.DefaultCredentials
Dim response As WebResponse = request.GetResponse()
Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
Dim dataStream As Stream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()

reader.Close()
response.Close()

Debug.Print("Downloaded. Saving to local Audio Cache...")

' Check and create the audio-cache directory

If (Not System.IO.Directory.Exists(WorkingDir + "\audio-cache\")) Then
    Debug.Print("Audio cache directory doesn't exist. Creating... ")
    System.IO.Directory.CreateDirectory(WorkingDir + "\audio-cache\")
    Debug.Print("Created at " + WorkingDir + "\audio-cache\")
End If

Dim Data As String = responseFromServer

' Save the mp3 file!

Dim fileLoc As String = WorkingDir + "\audio-cache\" + Mp3Hash + ".mp3"
Dim fs As FileStream = Nothing
If (Not File.Exists(fileLoc)) Then
    fs = File.Create(fileLoc)
    Using fs

    End Using
End If
If File.Exists(fileLoc) Then
    Using sw As StreamWriter = New StreamWriter(fileLoc)
        sw.Write(Data)
    End Using
End If

Debug.Print("Saved successfully to " + fileLoc)

How can I properly download, and locally save, an mp3 file from a URL?

如何从 URL 正确下载和本地保存 mp3 文件?

回答by Michael B.

try this

尝试这个

Dim HttpReq As HttpWebRequest = DirectCast(WebRequest.Create("http://...."), HttpWebRequest)

        Using HttpResponse As HttpWebResponse = DirectCast(HttpReq.GetResponse(), HttpWebResponse)
            Using Reader As New BinaryReader(HttpResponse.GetResponseStream())
                Dim RdByte As Byte() = Reader.ReadBytes(1 * 1024 * 1024 * 10)
                Using FStream As New FileStream("FileName.extension", FileMode.Create)
                    FStream.Write(RdByte, 0, RdByte.Length)
                End Using
            End Using
        End Using