views:

218

answers:

3

Hello,

I write a program wich download web pages. It works fine for most of web pages but i have found some pages where it doesn't work.

These pages contains 0x00 characters.

I'm able to read page content until this character, but not the content after.

I use this part of code to read the response :

IAsyncResult ar = null;
HttpWebResponse resp = null;
Stream responseStream = null;
String content = null;
...
resp = (HttpWebResponse)req.EndGetResponse(ar);
responseStream = resp.GetResponseStream();
StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
content = sr.ReadToEnd();

In this example i use asynchronous request, but i try with synchronous one and i have the same probleme.

I also try this with the same result :

HttpWebResponse resp = null;
Stream responseStream = null;
String content = new String();
...
responseStream = resp.GetResponseStream();
byte[] buffer = new byte[4096];
int bytesRead = 1;
while (bytesRead > 0)
{
    bytesRead = responseStream.Read(buffer, 0, 4096);
    content += Encoding.UTF8.GetString(buffer, 0, bytesRead);
}

for example, the problem occurs for this url http://www.daz3d.com/i/search/searchsub?sstring=ps%5Ftx1662b&%5Fm=dps%5Ftx1662b

thanks for yours responses

Euyeusu

A: 

Your problem is to transform received content to string, where you need to remove those 0x00 bytes:

AutoResetEvent sync = new AutoResetEvent(false);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://...");
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
request.BeginGetResponse((result) =>
{
    StringBuilder content = new StringBuilder();
    using (HttpWebResponse response = 
           request.EndGetResponse(result) as HttpWebResponse)
    using (Stream stream = response.GetResponseStream())
    {
        int read = 1;
        byte[] buffer = new byte[0x1000];
        while (read > 0)
        {
            read = stream.Read(buffer, 0, buffer.Length);
            content.Append(Encoding.UTF8.GetString(buffer
                .TakeWhile((b, index) => index <= read)
                .Where(b => b != 0x00).ToArray()));
        }
        Console.WriteLine(content);
        sync.Set();
    }
}, null);
sync.WaitOne();
Rubens Farias
A: 

Thanks to help me Rubens

But your solution doesn't resolve my problem.

The problem is not to transform byte into String but read 0x00 byte from stream. I can't remove 0x00 bytes because i can't read them from stream. The stream consider the 0x00 byte as the end of the stream.

Euyeusu
A: 

Hi,

It is the encoding that actually fails. To get around it you'll have to filter out the 0x00 bytes. Something like this should do the trick:

using System.Net;
using System.IO;
using System.Text;

WebRequest request = WebRequest.Create("url here");
WebResponse response = request.GetResponse();

string html;
using (Stream stream = response.GetResponseStream())
{

    int index = -1, currentByte = 0;
    byte[] buffer = new byte[response.ContentLength];
    while ((currentByte = stream.ReadByte()) > -1)
    {
        if(currentByte > 0) buffer[++index] = (byte)currentByte;
    }

    html = Encoding.ASCII.GetString(buffer, 0, index + 1);
}
Chris