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"))
{
responseStream = 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?