Ejemplo n.º 1
0
        public void ReceiveResponseEntity(HttpResponse response)
        {
            if (!this.IsConnected())
            {
                throw new HttpNetworkException("Socket is closed or not ready");
            }

            string chunkedHeader = EntityUtils.GetTransferEncoding(response.Headers);

            if (chunkedHeader != null &&
                chunkedHeader.ToLower().Equals(HTTP.CHUNK_CODING))
            {
                List <byte>     byteBuffer = new List <byte>();
                BasicHttpEntity httpEntity = new BasicHttpEntity();
                httpEntity.ContentLength = 0;
                httpEntity.ContentType   = EntityUtils.GetContentType(response.Headers);
                response.Entity          = httpEntity;

                int chunkSize = EntityUtils.ConvertHexToInt(ReceiveLine());
                while (chunkSize > 0)
                {
                    // for each chunk...
                    byteBuffer.AddRange(ReceiveBytes(chunkSize));
                    httpEntity.ContentLength += chunkSize;
                    string test = ReceiveLine();
                    chunkSize = EntityUtils.ConvertHexToInt(ReceiveLine());
                }

                httpEntity.Content = byteBuffer.ToArray();
            }
            else
            {
                // TODO: support "Transfer-Encoding: chunked"
                int length = EntityUtils.GetContentLength(response.Headers);
                if (length > 0)
                {
                    BasicHttpEntity httpEntity = new BasicHttpEntity();
                    httpEntity.ContentLength = length;
                    httpEntity.Content       = ReceiveBytes(length).ToArray();
                    httpEntity.ContentType   = EntityUtils.GetContentType(response.Headers);
                    response.Entity          = httpEntity;
                }
                else
                {
                    byte[] content = ReceiveToEnd();
                    length = content.Length;
                    if (length > 0)
                    {
                        BasicHttpEntity httpEntity = new BasicHttpEntity();
                        httpEntity.ContentLength = length;
                        httpEntity.Content       = content;
                        httpEntity.ContentType   = EntityUtils.GetContentType(response.Headers);
                        response.Entity          = httpEntity;
                    }
                }
            }

            return;
        }