我遇到了代理http流的问题。
首先:我正在使用VLC播放器创建http协议媒体流服务器。
第二:我正在使用HttpListener在一个端口上侦听http请求,并试图从vlc服务器端口转发响应作为第一个响应。
代理:
Client Server(:1234) VLC(:2345)
-request-> HttpListener
HttpWebRequest -request->
HttpWebResponse <-response-
Stream <=Copy= Stream
<-response- HttpListenerResponse
一切正常。但仍有一个问题。我正在尝试将实时流复制到HttpListenerResponse。但我不能将负值附加到其属性ContentLength64。 HttpWebResponse ContentLength属性的值为-1。它应该是无限长度内容的值。
这是必要的,因为我正在转发直播。
void ProxyRequest(HttpListenerResponse httpResponse)
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://localhost:2345");
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
// this must be >=0. Throws ArgumentOutOfRangeException "The value specified for a set operation is less than zero."
httpResponse.ContentLength64 = HttpWResp.ContentLength;
byte[] buffer = new byte[32768];
int bytesWritten = 0;
while (true)
{
int read = HttpWResp.GetResponseStream().Read(buffer, 0, buffer.Length);
if (read <= 0)
break;
httpResponse.OutputStream.Write(buffer, 0, read);
bytesWritten += read;
}
}
有没有人能解决这个问题?
答案 0 :(得分:2)
将SendChunked属性设置为true并删除ContentLength64值分配应该是解决方案。就像你提供的链接中描述的一样。
void ProxyRequest(HttpListenerResponse httpResponse)
{
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create("http://localhost:2345");
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
// Solution!!!
httpResponse.SendChunked = true;
byte[] buffer = new byte[32768];
int bytesWritten = 0;
while (true)
{
int read = HttpWResp.GetResponseStream().Read(buffer, 0, buffer.Length);
if (read <= 0)
break;
httpResponse.OutputStream.Write(buffer, 0, read);
bytesWritten += read;
}
}