我正在为 Windows 8.1 制作一个在线广播应用程序,并希望使用新Windows.Web.Http API
与shoutcast
服务器进行通信(以便发送自定义 HTTP 标头以从实时媒体流中获取元数据)。
响应标头为空,我需要在开始读取流数据之前读取内容标头。
这是我尝试使用的代码:
Uri uri = new Uri(Url);
var baseFilter = new HttpBaseProtocolFilter();
ShoutcastHttpFilter filter = new ShoutcastHttpFilter(baseFilter);
var HClient = new HttpClient(filter);
HttpResponseMessage response = await HClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead);
Task streaming = null;
Stream stream = null;
stream = response.Content.ReadAsInputStreamAsync().GetResults().AsStreamForRead();
string str = response.Content.Headers["Icy-MetaInt"];
当我运行/调试代码时,内容显示为"未缓冲"并且没有标头。如何获取内容标头和流?
这是我在ShoutcastHttpFilter中使用的代码:
request.Headers.Clear();
request.Headers.Add("Icy-MetaData", "1");
request.Headers["User-Agent"] = "VLC media player";
request.Headers["Connection"] = "Close";
HttpResponseMessage response = await innerFilter.SendRequestAsync(request).AsTask(cancellationToken, progress);
cancellationToken.ThrowIfCancellationRequested();
return response;
如果标头名称不以 Content-*
开头,则标头必须位于响应标头中。
这样做:
Uri uri = new Uri("http://example.com");
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(
uri,
HttpCompletionOption.ResponseHeadersRead);
string value = response.Headers["Icy-MetaInt"];