tags:

views:

150

answers:

4

Hi, I'm from the Urkraine, and have bad english, but anyway not sure if there is an answer on my question.

I took example from [here][1] but i have exception that GZip magical number is not valid, why ?

    public long WriteUrl()
    {
        long num1 = 0;
        bool saveItAtCache = false;
        bool existsAtCache = false;
        byte[] cachedFile = null;
        string ext = Path.GetExtension(_url).ToLower();
        if (!_url.Contains(".php") && ".gif.jpg.swf.js.css.png.html".IndexOf(ext) != -1 && ext != "")
        {
            saveItAtCache = true;
            cachedFile = cache.GetFile(_url);
            existsAtCache = (cachedFile != null);
        }
        if (existsAtCache)
        {
            writeSuccess(cachedFile.Length, null);
            socket.Send(cachedFile);
        }
        string host = new Uri(_url).Host;
        IPHostEntry ipAddress = Dns.GetHostEntry(host);
        IPEndPoint ip = new IPEndPoint(ipAddress.AddressList[0], 80);
        using (Socket s = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
        {
            s.Connect(ip);
            using (NetworkStream n = new NetworkStream(s))
            {
                if (HttpRequestType == "GET")
                {
                    SendRequest(n, new[] { socketQuery});
                }
                Dictionary<string, string> headers = new Dictionary<string, string>();
                while (true)
                {
                    string line = ReadLine(n);
                    if (line.Length == 0)
                    {
                        break;
                    }
                    int index = line.IndexOf(':');
                    if (!headers.ContainsKey(line.Substring(0, index)))
                    {
                        headers.Add(line.Substring(0, index), line.Substring(index + 2));
                    }
                }

                string contentEncoding;
                if (headers.TryGetValue("Content-Encoding", out contentEncoding))
                {
                    Stream responseStream = n;
                    if (contentEncoding.Equals("gzip"))
                    {
                        responseStream = new GZipStream(responseStream, CompressionMode.Decompress, true);
                    }
                    else if (contentEncoding.Equals("deflate"))
                    {
                        responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
                    }

                    var memStream = new MemoryStream();

                    var respBuffer = new byte[4096];
                    try
                    {
                        int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
                        //int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
                        while (bytesRead > 0)
                        {
                            memStream.Write(respBuffer, 0, bytesRead);
                            bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
                        }
                    }
                    finally
                    {
                        responseStream.Close();
                    }
                    string str = encoding.GetString(memStream.ToArray());
                    ManageCookies(headers["Set-Cookie"], _headers["Host"]);
                    cachedFile = encoding.GetBytes(str);
                    if (saveItAtCache)
                    {
                        cache.Store(_url, cachedFile);
                    }
                    writeSuccess(cachedFile.Length, headers["Set-Cookie"]);
                    socket.Send(cachedFile);
                    num1 = str.Length;
                }
                else
                {
                    while (true)
                    {
                        string line = ReadLine(n);
                        if (line == null)
                        {
                            break;
                        }
                        num1 = line.Length;
                    }
                }
            }
        }

        return num1;
    }
A: 

methods SendRequest and ReadLine you can view on link that i gave in my first message

IICuX
Upul
A: 

You might want to check this question as its very similar.

GrayWizardx
A: 

In these lines

                string str = encoding.GetString(memStream.ToArray());
                ManageCookies(headers["Set-Cookie"], _headers["Host"]);
                cachedFile = encoding.GetBytes(str);

You're converting the byte array to a string and then back to a byte array. Since the original data is a gzip or jpg or whatever and not really a string, this conversion is probably screwing it up. I don't see you using str at all, so just take it out (use cachedFile.Length when you need the length instead of str.Length).

Sam
A: 

sorry, link here.

Sam, I have problem in this line

int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length); // Exception, that GZip magical number is not valid 

    void SendRequest(Stream stream, IEnumerable<string> request) { 
        foreach (var r in request) { 
            var data = DefaultEncoding.GetBytes(r); 
            stream.Write(data, 0, data.Length); 
            stream.Write(LineTerminator, 0, 2); // this line a think a GZip magical number
        } 
        stream.Write(LineTerminator, 0, 2); // and this line a think a GZip magical number
        // Eat response 
        var response = ReadLine(stream); 
    } 

    string ReadLine(Stream stream) { 
        var lineBuffer = new List<byte>(); 
        while (true) { 
            int b = stream.ReadByte(); 
            if (b == -1) { 
                return null; 
            } 
            if (b == 10) { 
                break; 
            } 
            if (b != 13) { 
                lineBuffer.Add((byte)b); 
            } 
        } 
        return DefaultEncoding.GetString(lineBuffer.ToArray()); 
    } 

and 2 methods

IICuX