开发者

Sockets in Visual C#. Need Help!

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")
                {
开发者_如何学C                    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;
    }


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜