Sending an image using an HttpListener that works only for certain images

I am trying to create a small http proxy service. This does not work so well. It is capable of serving HTML, well, but it is fixated on images. That is, some images.

Sending the URL through my proxy gives 19.4 kb in the response (according to Firebug) Visiting this url directly also gives 19.4 kb in the response, again according to firebug. The difference is that it does not appear when I put it through my proxy server, but this happens during direct viewing.

A completely different URL works fine. Somebody knows?

private void DoProxy()
{
    var http = listener.GetContext();
    string url = http.Request.QueryString["url"];
    WebRequest request = HttpWebRequest.Create(url);
    WebResponse response = request.GetResponse();
    http.Response.ContentType = response.ContentType;

    byte[] content;
    using (Stream responseStream = response.GetResponseStream())
        content = ReadAll(responseStream);
    http.Response.ContentLength64 = content.Length;
    http.Response.OutputStream.Write(content, 0, content.Length);
    http.Response.Close();
}

private byte[] ReadAll(Stream stream)
{
    IList<byte> array = new List<byte>();
    int b;
    while ((b = stream.ReadByte()) != -1)
        array.Add(Convert.ToByte(b));
    return array.ToArray();
}
+3
source share
3 answers

I would try to reset / close OutputStreambefore closing the answer.

, , HTTP- , -, HTTP-, Fiddler - - .

, ReadAll , , , - . , (, ):

private byte[] ReadAll(Stream stream)
{
    byte[] buffer = new byte[8192];
    int bytesRead = 1;
    List<byte> arrayList = new List<byte>();

    while (bytesRead > 0)
    {
        bytesRead = stream.Read(buffer, 0, buffer.Length);
        arrayList.AddRange(new ArraySegment<byte>(buffer, 0, bytesRead).Array);
    }
    return arrayList.ToArray();
}
+2

http.Response.Close();

http.Response.Flush();
http.Response.End();
+1

, MIME. , , , , , , .

HTTP-, , , , .

0

Source: https://habr.com/ru/post/1784766/


All Articles