无法在C#中通过http传输Jpegs图像

时间:2022-03-06 15:54:36

I 've a stream of Jpegs images and I'm trying to stream them with HTTP to VLC

我有一个Jpegs图像流,我正在尝试使用HTTP将它们流式传输到VLC

The code if the following:

代码如下:

 public HttpListenerResponse StartListening(String where)
    {
        listener = new HttpListener();
        listener.Start();
        Console.WriteLine("listening on " + where);
        listener.Prefixes.Add(where);
        HttpListenerContext context = listener.GetContext();
        HttpListenerRequest request = context.Request;
        // Obtain a response object.
        HttpListenerResponse response = context.Response;
        return response;
    }
public void StartStream()
    {
        HttpListenerResponse response = StartListening("http://localhost:8080/");
        MemoryStream mem = null;

        for (;;)
        {
            Bitmap b = generateBitmap();
            //pictureBox1.Image = frm.GetFormImage();
            byte[] ar = BitmapToArray(b);


            // Construct a response.
            byte[] buffer = ar;
            response.ContentType = "multipart/x-mixed-replace; boundary=--testboundary";
            ASCIIEncoding ae = new ASCIIEncoding();
            byte[] boundary = ae.GetBytes("\r\n--testboundary\r\nContent-Type: image/jpeg\r\nContent-Length:" + buffer.Length + "\r\n\r\n");
            mem = new MemoryStream(boundary);
            mem.WriteTo(response.OutputStream);
            mem = new MemoryStream(buffer);
            mem.WriteTo(response.OutputStream);
        }   
        mem.Close();
        listener.Stop();    

    }

For some reason, If i try to open the stream in firefox it comes back all black, If i try to open it with VLC i get "Unable to open stream"

出于某种原因,如果我尝试在firefox中打开流,它会全部变回黑色,如果我尝试用VLC打开它,我会得到“无法打开流”

If i only stream 1 image, firefox has no problem dealing with it.

如果我只流1个图像,firefox处理它没有问题。

Tks in advance, Jose

提前说,何塞

2 个解决方案

#1


You may give a multi-part range request a try. VLC may be requesting the data in 'chunks' but your HTTPHandler isn't providing that functionality. Here's some (lengthy) example code below. I had a similar problem when trying to stream music to my iPod Touch. Worked like a charm in brower, but didn't work with the Touch. Turns out, the iPod was requesting byte[] chunks via multiple requests to my handler, but the handler didn't implement the functionality it needed.

您可以尝试提供多部分范围请求。 VLC可能正在以“块”请求数据,但您的HTTPHandler未提供该功能。下面是一些(冗长的)示例代码。尝试将音乐流式传输到iPod Touch时,我遇到了类似的问题。在浏览器中工作就像一个魅力,但没有与Touch一起工作。事实证明,iPod通过对我的处理程序的多个请求请求byte []块,但处理程序没有实现它所需的功能。

I got some of my own library classes mingled in here, but you'll get the picture well enough.

我在这里混合了一些自己的库类,但是你会得到足够的图片。

public abstract class MultipartRangeHandler : IHttpHandler
{
    protected const String HEADER_RANGE = "range";
    protected const String HEADER_CONTENT_TYPE = "Content-Type";
    protected const String HEADER_CONTENT_LENGTH = "Content-Length: ";
    protected const String HEADER_CONTENT_DISPOSITION = "Content-Disposition";
    protected const String HEADER_CONTENT_RANGE = "Content-Range";
    protected const String HEADER_BOUNDARY_DELIMETER = "--";
    protected const String HEADER_STATUS_PARTIAL_CONTENT = "Partial Content";

    private const char COMMA = ',';
    private const char EQUALS = '=';
    private const char NEW_LINE = '\n';

    protected const String QS_OBJECT_ID = "cid";

    public void ProcessRequest(HttpContext context)
    {
        if (StringUtils.isNullOrEmpty(context.Request.QueryString[QS_OBJECT_ID]))
        {
            sendResponse(400, "400 Bad Request", "No resource was specified in the query string to retrieve.", context);
        }
        else
        {
            ContentItem contentItem = getContentItem(context.Request.QueryString[QS_OBJECT_ID]);

            if (contentItem != null)
            {
                context.Response.Clear();
                context.Response.ClearHeaders();
                context.Response.ClearContent();

                if (context.Request.Headers[HEADER_RANGE] != null)
                {

                    string range = context.Request.Headers[HEADER_RANGE];
                    range = range.Substring(range.LastIndexOf(EQUALS) + 1);
                    bool isMultipartRange = range.Contains(COMMA.ToString());

                    if (!isMultipartRange)
                    {
                        addHeader(context.Response, HEADER_CONTENT_TYPE, contentItem.MimeType);
                        addHeader(context.Response, HEADER_CONTENT_DISPOSITION, "inline; filename=\"" + contentItem.Filename + "\"");

                        string[] startEnd = range.Split('-');

                        long startPos;

                        long.TryParse(startEnd[0], out startPos);

                        long endPos;
                        int fileSize = contentItem.FileBytes.Length;

                        if (startEnd.GetUpperBound(0) >= 1 && startEnd[1] != String.Empty)
                        {
                            long.TryParse(startEnd[1], out endPos);
                        }
                        else
                        {
                            endPos = fileSize - startPos;
                        }

                        if (endPos > fileSize)
                        {
                            endPos = fileSize - startPos;
                        }

                        context.Response.StatusCode = 206;
                        context.Response.StatusDescription = HEADER_STATUS_PARTIAL_CONTENT;
                        addHeader(context.Response, HEADER_CONTENT_RANGE, "bytes " + startPos + "-" + endPos + "/" + fileSize);

                        context.Response.BinaryWrite(ByteUtils.subByte(contentItem.FileBytes, (int)startPos, (int)(endPos - startPos) + 1));
                    }
                    else
                    {

                        string boundary = "waynehartmanansmach";
                        addHeader(context.Response, HEADER_CONTENT_TYPE, "multipart/byteranges; boundary=" + boundary);

                        List<string[]> ranges = new List<string[]>();
                        string[] multiRange = range.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string mr in multiRange)
                        {
                            ranges.Add(mr.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries));
                        }

                        //  process the list of ranges
                        foreach (string[] rangeArray in ranges.ToArray())
                        {
                            context.Response.Write(HEADER_BOUNDARY_DELIMETER + boundary + NEW_LINE);
                            context.Response.Write(HEADER_CONTENT_TYPE + ": " + contentItem.MimeType + NEW_LINE);
                            context.Response.Write(HEADER_CONTENT_RANGE + ": bytes " + rangeArray[0] + "-" + rangeArray[1] + "/" + contentItem.FileBytes + NEW_LINE + NEW_LINE);
                            long startPos = long.Parse(rangeArray[0]);
                            long endPos = long.Parse(rangeArray[1]);

                            context.Response.BinaryWrite(ByteUtils.subByte(contentItem.FileBytes, (int)startPos, (int)(endPos - startPos) + 1));

                            context.Response.Write(NEW_LINE);
                            context.Response.Flush();
                        }

                        context.Response.Write(HEADER_BOUNDARY_DELIMETER + boundary + HEADER_BOUNDARY_DELIMETER + NEW_LINE + NEW_LINE);
                    }
                }
                else
                {
                    context.Response.ContentType = contentItem.MimeType;
                    addHeader(context.Response, HEADER_CONTENT_DISPOSITION, "attachment; filename=\"" + contentItem.Filename + "\"");
                    addHeader(context.Response, HEADER_CONTENT_LENGTH, contentItem.FileBytes.Length.ToString());

                    context.Response.OutputStream.Write(contentItem.FileBytes, 0, contentItem.FileBytes.Length);
                }
            }
            else
            {
                sendResponse(404, "404 Not Found", "The resource requested does not exist.", context);                    
            }
        }
    }

    private void sendResponse(int statusCode, String status, String statusMessage, HttpContext context)
    {
        System.Text.StringBuilder data = new System.Text.StringBuilder();

        data.AppendLine("<html><body>");
        data.AppendLine("<h1>"+status+"</h1>");
        data.AppendLine("<p>"+statusMessage+"</p>");
        data.AppendLine("</body></html>");

        byte[] headerData = System.Text.Encoding.ASCII.GetBytes(data.ToString());

        context.Response.ContentType = "text/html";
        context.Response.StatusCode = statusCode;
        context.Response.Status = status;

        addHeader(context.Response, HEADER_CONTENT_LENGTH, headerData.Length.ToString());

        //context.Response.AddHeader("Content-Length: ", headerData.Length.ToString());
        context.Response.OutputStream.Write(headerData, 0, headerData.Length);
        context.Response.End();
    }

    protected void addHeader(HttpResponse response, String key, String value)
    {
        response.AddHeader(key, value);
    }

    protected abstract com.waynehartman.util.web.handlers.multipartrange.ContentItem getContentItem(String objectID);

    public bool IsReusable
    {
        get { return true; }
    }
}

#2


Set your prefixes, then call Start().

设置前缀,然后调用Start()。

From what I've seen in the code and elsewhere, you can't stream data using HttpListener. You might have to forward the request to another local TCP socket that writes the data back.

从我在代码和其他地方看到的情况来看,你不能使用HttpListener来传输数据。您可能必须将请求转发到另一个将数据写回的本地TCP套接字。

#1


You may give a multi-part range request a try. VLC may be requesting the data in 'chunks' but your HTTPHandler isn't providing that functionality. Here's some (lengthy) example code below. I had a similar problem when trying to stream music to my iPod Touch. Worked like a charm in brower, but didn't work with the Touch. Turns out, the iPod was requesting byte[] chunks via multiple requests to my handler, but the handler didn't implement the functionality it needed.

您可以尝试提供多部分范围请求。 VLC可能正在以“块”请求数据,但您的HTTPHandler未提供该功能。下面是一些(冗长的)示例代码。尝试将音乐流式传输到iPod Touch时,我遇到了类似的问题。在浏览器中工作就像一个魅力,但没有与Touch一起工作。事实证明,iPod通过对我的处理程序的多个请求请求byte []块,但处理程序没有实现它所需的功能。

I got some of my own library classes mingled in here, but you'll get the picture well enough.

我在这里混合了一些自己的库类,但是你会得到足够的图片。

public abstract class MultipartRangeHandler : IHttpHandler
{
    protected const String HEADER_RANGE = "range";
    protected const String HEADER_CONTENT_TYPE = "Content-Type";
    protected const String HEADER_CONTENT_LENGTH = "Content-Length: ";
    protected const String HEADER_CONTENT_DISPOSITION = "Content-Disposition";
    protected const String HEADER_CONTENT_RANGE = "Content-Range";
    protected const String HEADER_BOUNDARY_DELIMETER = "--";
    protected const String HEADER_STATUS_PARTIAL_CONTENT = "Partial Content";

    private const char COMMA = ',';
    private const char EQUALS = '=';
    private const char NEW_LINE = '\n';

    protected const String QS_OBJECT_ID = "cid";

    public void ProcessRequest(HttpContext context)
    {
        if (StringUtils.isNullOrEmpty(context.Request.QueryString[QS_OBJECT_ID]))
        {
            sendResponse(400, "400 Bad Request", "No resource was specified in the query string to retrieve.", context);
        }
        else
        {
            ContentItem contentItem = getContentItem(context.Request.QueryString[QS_OBJECT_ID]);

            if (contentItem != null)
            {
                context.Response.Clear();
                context.Response.ClearHeaders();
                context.Response.ClearContent();

                if (context.Request.Headers[HEADER_RANGE] != null)
                {

                    string range = context.Request.Headers[HEADER_RANGE];
                    range = range.Substring(range.LastIndexOf(EQUALS) + 1);
                    bool isMultipartRange = range.Contains(COMMA.ToString());

                    if (!isMultipartRange)
                    {
                        addHeader(context.Response, HEADER_CONTENT_TYPE, contentItem.MimeType);
                        addHeader(context.Response, HEADER_CONTENT_DISPOSITION, "inline; filename=\"" + contentItem.Filename + "\"");

                        string[] startEnd = range.Split('-');

                        long startPos;

                        long.TryParse(startEnd[0], out startPos);

                        long endPos;
                        int fileSize = contentItem.FileBytes.Length;

                        if (startEnd.GetUpperBound(0) >= 1 && startEnd[1] != String.Empty)
                        {
                            long.TryParse(startEnd[1], out endPos);
                        }
                        else
                        {
                            endPos = fileSize - startPos;
                        }

                        if (endPos > fileSize)
                        {
                            endPos = fileSize - startPos;
                        }

                        context.Response.StatusCode = 206;
                        context.Response.StatusDescription = HEADER_STATUS_PARTIAL_CONTENT;
                        addHeader(context.Response, HEADER_CONTENT_RANGE, "bytes " + startPos + "-" + endPos + "/" + fileSize);

                        context.Response.BinaryWrite(ByteUtils.subByte(contentItem.FileBytes, (int)startPos, (int)(endPos - startPos) + 1));
                    }
                    else
                    {

                        string boundary = "waynehartmanansmach";
                        addHeader(context.Response, HEADER_CONTENT_TYPE, "multipart/byteranges; boundary=" + boundary);

                        List<string[]> ranges = new List<string[]>();
                        string[] multiRange = range.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string mr in multiRange)
                        {
                            ranges.Add(mr.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries));
                        }

                        //  process the list of ranges
                        foreach (string[] rangeArray in ranges.ToArray())
                        {
                            context.Response.Write(HEADER_BOUNDARY_DELIMETER + boundary + NEW_LINE);
                            context.Response.Write(HEADER_CONTENT_TYPE + ": " + contentItem.MimeType + NEW_LINE);
                            context.Response.Write(HEADER_CONTENT_RANGE + ": bytes " + rangeArray[0] + "-" + rangeArray[1] + "/" + contentItem.FileBytes + NEW_LINE + NEW_LINE);
                            long startPos = long.Parse(rangeArray[0]);
                            long endPos = long.Parse(rangeArray[1]);

                            context.Response.BinaryWrite(ByteUtils.subByte(contentItem.FileBytes, (int)startPos, (int)(endPos - startPos) + 1));

                            context.Response.Write(NEW_LINE);
                            context.Response.Flush();
                        }

                        context.Response.Write(HEADER_BOUNDARY_DELIMETER + boundary + HEADER_BOUNDARY_DELIMETER + NEW_LINE + NEW_LINE);
                    }
                }
                else
                {
                    context.Response.ContentType = contentItem.MimeType;
                    addHeader(context.Response, HEADER_CONTENT_DISPOSITION, "attachment; filename=\"" + contentItem.Filename + "\"");
                    addHeader(context.Response, HEADER_CONTENT_LENGTH, contentItem.FileBytes.Length.ToString());

                    context.Response.OutputStream.Write(contentItem.FileBytes, 0, contentItem.FileBytes.Length);
                }
            }
            else
            {
                sendResponse(404, "404 Not Found", "The resource requested does not exist.", context);                    
            }
        }
    }

    private void sendResponse(int statusCode, String status, String statusMessage, HttpContext context)
    {
        System.Text.StringBuilder data = new System.Text.StringBuilder();

        data.AppendLine("<html><body>");
        data.AppendLine("<h1>"+status+"</h1>");
        data.AppendLine("<p>"+statusMessage+"</p>");
        data.AppendLine("</body></html>");

        byte[] headerData = System.Text.Encoding.ASCII.GetBytes(data.ToString());

        context.Response.ContentType = "text/html";
        context.Response.StatusCode = statusCode;
        context.Response.Status = status;

        addHeader(context.Response, HEADER_CONTENT_LENGTH, headerData.Length.ToString());

        //context.Response.AddHeader("Content-Length: ", headerData.Length.ToString());
        context.Response.OutputStream.Write(headerData, 0, headerData.Length);
        context.Response.End();
    }

    protected void addHeader(HttpResponse response, String key, String value)
    {
        response.AddHeader(key, value);
    }

    protected abstract com.waynehartman.util.web.handlers.multipartrange.ContentItem getContentItem(String objectID);

    public bool IsReusable
    {
        get { return true; }
    }
}

#2


Set your prefixes, then call Start().

设置前缀,然后调用Start()。

From what I've seen in the code and elsewhere, you can't stream data using HttpListener. You might have to forward the request to another local TCP socket that writes the data back.

从我在代码和其他地方看到的情况来看,你不能使用HttpListener来传输数据。您可能必须将请求转发到另一个将数据写回的本地TCP套接字。