GZipStream, how correctly read from GZipStream
I have some code writen by me in C#
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))
{
byte[] write = encoding.GetBytes(HttpQuery);
n.Write(write, 0, write.Length);
ReadLine(n);
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"))
{
responseSt开发者_开发技巧ream = new GZipStream(responseStream, CompressionMode.Decompress);
responseStream.Flush();
}
else if (contentEncoding.Equals("deflate"))
{
responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
}
MemoryStream memStream = new MemoryStream();
byte[] respBuffer = new byte[4096];
try
{
int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
//int bytesRead = responseStream.ReadByte();
while (bytesRead > 0)
{
memStream.Write(respBuffer, 0, bytesRead);
bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
}
}
finally
{
responseStream.Close();
}
string str = encoding.GetString(memStream.ToArray());
Then I Have an exception InvalidDataException in this line int bytesRead = responseStream.Read(respBuffer, 0, respBuffer.Length);
GZip header magic number is not correct.
string ReadLine(Stream stream)
{
List<byte> lineBuffer = new List<byte>();
try
{
while (true)
{
int b = stream.ReadByte();
if (b == -1)
return null;
if (b == 10)
break;
if (b != 13)
lineBuffer.Add((byte) b);
}
}
catch (Exception)
{
}
return encoding.GetString(lineBuffer.ToArray());
}
Any ideas?
Your ReadLine function returns as soon as it reads one line feed character when reading a blank line. Doesn't that potentially leave the stream positioned at a carriage return character instead of at the beginning of the GZip data stream?
in general, I found something on the subject, here's Sockets in C#: How to get the response stream? link, there is some information that before the string (s) to do so:
Stream responseStream = n;
int magicNumber = 0;
while (magicNumber != 10)
{
magicNumber = responseStream.ReadByte();
}
if (contentEncoding.Equals("gzip"))
{
responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
}
perhaps I did not quite do it correctly?
nobugz I have very succeful code writen with WttWebRequest and HttpWebResponse, BUT my page have codepape-1251, and HttpWebRequest convert url from 1251 encoding to UTF-8 I and nothing can do with this, maybe you have any ideas?
For example I have url as http://myurl.php?name=Мастер%20Создатель
HttpWebRequest convert this in http://myurl.php?name=%d0%9c%d0%b0%d1%81%d1%82%d0%b5%d1%80+%d0%a1%d0%be%d0%b7%d0%b4%d0%b0%d1%82%d0%b5%d0%bb%d1%8c (UTF-8)
BUT will be http://myurl.php?name=%CC%E0%F1%F2%E5%F0%20%D1%EE%E7%E4%E0%F2%E5%EB%FC(windows-1251), I don't know how fix this
精彩评论