V
V
Vitaly Pukhov2015-02-06 02:39:43
ASP.NET
Vitaly Pukhov, 2015-02-06 02:39:43

Why does the browser freeze when loading a page?

There is a crazy page on ASP.net, the experiment, so to speak, its essence is as follows, it takes parameters from the url (u-url, s-start byte, e-last byte), after which it loads the necessary bytes from the file specified by url and encodes them in base64, after which the text is displayed in response as a response.
everything in debugging works fine until exiting the Load() procedure, then the browser should receive a response and simply display it on the page, there are no problems with plain text, but for some reason the browser freezes to death and sometimes pulls the studio along with it (CPU load = 0) what could be wrong? (apart from the delusional idea, but I assure you that there is a sacred meaning)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string url="";
            string start_s="";
            string end_s="";

            Response.Clear(); // clear response buffer
            if (Request.QueryString["u"] != null) 
            {
                url = Request.QueryString["u"];
            }
            if (Request.QueryString["s"] != null)
            {
                start_s = Request.QueryString["s"];
            }
            if (Request.QueryString["e"] != null)
            {
                end_s = Request.QueryString["e"];
            }
            if (url!="")
            {
                int start = 0;
                int end = 0;
                if (int.TryParse(start_s, out start))
                {
                    if (int.TryParse(end_s , out end))
                    {
                        if (Download(url,start,end))
                        {
                            Response.End();
                            return;
                        }
                        else
                        {
                            Response.Clear();
                            Response.End();
                            return;
                        }
                    }
                }
            }

                //Response.Write();
            //Response.Write("test"); // write your new text
            Response.End();
        }

        private bool Download(string url, int start,int end)
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                //string proxyIp = "8080";
                //string proxyPort = "127.0.0.1";
                //string urlProxy = "http://127.0.0.1:8080/";
                //WebProxy proxyObject = new WebProxy(urlProxy);
                //req.Proxy = proxyObject;
                req.Timeout = 5000;
                req.ReadWriteTimeout = 5000;
                byte[] buffer;

                //req.UseDefaultCredentials = true;
                //req.Proxy = new WebProxy();
                //req.Timeout = 1000;
                req.Method = "GET";
                req.AddRange(start, end); // запрашиваем диапазон в первые 1001 байт
                var resp = req.GetResponse();
                var stream = resp.GetResponseStream();

                //stream.ReadTimeout = 8000;
                //stream.Read(buffer, 0, (int)(end - start));
                int contentLength = Convert.ToInt32(resp.ContentLength);

                buffer = StreamToByteArray(stream, contentLength / 10, contentLength);
                string response = GetResponseString(buffer);
                stream.Close();
                resp.Close();
                buffer = null;
                Response.Write(response);
                return true;
            }
            catch (Exception)
            {
                Response.Clear();
                return false;
            }
        }

        public string GetResponseString(byte[] data)
        {
            string temp_inBase64 = Convert.ToBase64String(data);
            return temp_inBase64;
        }

        public byte[] GetDataFromB64(string response)
        {
            byte[] temp_backToBytes = Convert.FromBase64String(response);
            return temp_backToBytes;
        }
        public Stream ConvertToBase64(Stream stream)
        {
            Byte[] inArray = new Byte[(int)stream.Length];
            Char[] outArray = new Char[(int)(stream.Length * 1.34)];
            stream.Read(inArray, 0, (int)stream.Length);
            Convert.ToBase64CharArray(inArray, 0, inArray.Length, outArray, 0);
            return new MemoryStream(Encoding.UTF8.GetBytes(outArray));
        }

        byte[] StreamToByteArray(Stream stream, int initialLength, int chunksize)
        {
            // If we've been passed an unhelpful initial length, just
            // use 32K.
            if (initialLength < 1)
            {
                initialLength = 32768;
            }

            byte[] buffer = new byte[initialLength];
            int read = 0;

            int chunk;
            int last = 0;
            while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
            {
                read += chunk;

                // If we've reached the end of our buffer, check to see if there's
                // any more information

                double d = (double)read;
                d = d / (double)(chunksize);
                d = d * 100;
                d = Math.Ceiling(d);

                if (d - last > 10)
                {
                    last = (int)(d);
                    
                }

                if (read == buffer.Length)
                {
                    int nextByte = stream.ReadByte();

                    // End of stream? If so, we're done
                    if (nextByte == -1)
                    {
                        
                        return buffer;
                    }

                    // Nope. Resize the buffer, put in the byte we've just
                    // read, and continue
                    byte[] newBuffer = new byte[buffer.Length * 2];
                    Array.Copy(buffer, newBuffer, buffer.Length);
                    newBuffer[read] = (byte)nextByte;
                    buffer = newBuffer;
                    read++;
                }
            }
            // Buffer is now too big. Shrink it.
            byte[] ret = new byte[read];
            Array.Copy(buffer, ret, read);
            return ret;
        }

    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AxisPod, 2015-02-06
@AxisPod

And why should he not hang in your case? With your loading, you are blocking the work of the main thread in which the GUI is spinning, which is what you still wanted. Make at least a BackgroundWorker, or deal with asynchrony.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question