我正在 Visual C# 上制作程序,尝试连接到 Shoutcast 收音机并下载音频。但是,我在 GetResponse() 函数上收到协议违规错误。听说 Shoutcast 不是常见的 HTTP 协议,也没有找到特殊连接相关的资料。我的代码中有这样的东西:
private void downloadPart()
{
System.Net.WebRequest conn;
System.Net.WebResponse connResp;
System.IO.Stream readStream = null;
System.IO.FileStream writeStream;
int bufferSize = 8192;
try
{
conn = System.Net.WebRequest.Create(this.caminhoURL);
connResp = conn.GetResponse();
readStream = connResp.GetResponseStream();
writeStream = new System.IO.FileStream(@"C:\", System.IO.FileMode.Create, System.IO.FileAccess.Write);
int length;
byte[] buffer = new Byte[bufferSize];
do
{
length = readStream.Read(buffer, 0, bufferSize);
writeStream.Write(buffer, 0, length);
System.Diagnostics.Debug.WriteLine("Working");
readStream.Flush();
} while (length > 0);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.StackTrace);
}
finally
{
if (fluxoLeitura != null)
{
fluxoLeitura.Close();
System.Diagnostics.Debug.WriteLine("Done");
}
}
}
我已经测试了这个函数来下载一个简单的文件并且它有效。我知道流是连续的,所以我不能在一个文件中全部下载,我只需要知道如何配置连接。我该怎么做?