HttpWebRequest - data is truncated?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Logician

    HttpWebRequest - data is truncated?

    I am looking for some help about the method below which partly works.
    The problem is that the data returned is truncated in that the
    returned data has missing bytes near the beginning of the data. I am
    using VS 2005 and C#.

    Has anyone got any ideas?

    public string getWebPage(stri ng url)
    {
    // add error handling
    StringBuilder sb = new StringBuilder() ;
    byte[] buf = new byte[8192];
    try
    {
    HttpWebRequest request = (HttpWebRequest )WebRequest.Cre ate(url);
    request.UserAge nt = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de-
    DE;"+
    "rv:1.7.5) Gecko/20041108 Firefox/1.0";
    request.Timeout =20000;
    HttpWebResponse response = (HttpWebRespons e)request.GetRe sponse();
    Stream responseStream = response.GetRes ponseStream();
    StreamReader myTextReader = new StreamReader(re sponseStream);
    char[] strBuffer = new char[25];
    myTextReader.Re adBlock(strBuff er,0,25);
    string stringBuffer = new string(strBuffe r);
    if (stringBuffer.I ndexOf("GIF8")>-1 ||
    stringBuffer.In dexOf("JFIF")>-1)
    {
    // image found
    return "image";
    }
    else
    {
    // this is a text page
    }


    string tempString = null;
    int count = 0;
    do
    {
    count = responseStream. Read(buf,0,buf. Length);
    if (count!=0)
    {
    tempString=Enco ding.ASCII.GetS tring(buf,0,cou nt);
    sb.Append(tempS tring);
    }
    }while (count>0);
    }
    catch (WebException e)
    {
    return "ZenoBot getWebPage web failed to fetch "+e.ToString(); ;
    }
    catch (Exception e)
    {
    return "ZenoBot getWebPage web failed to fetch "+e.ToString(); ;
    }

    return sb.ToString();
    }


    }
  • Jon Skeet [C# MVP]

    #2
    Re: HttpWebRequest - data is truncated?

    Logician <sales@logician s.comwrote:
    I am looking for some help about the method below which partly works.
    The problem is that the data returned is truncated in that the
    returned data has missing bytes near the beginning of the data. I am
    using VS 2005 and C#.
    The StreamReader is probably buffering data - reading more than 25
    bytes and then saving it in its buffer.

    Instead of using a StreamReader, just use the Stream directly to start
    with. You're interested in the bytes which represent "GIF8", not the
    actual *text* "GIF8", right?

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    World class .NET training in the UK: http://iterativetraining.co.uk

    Comment

    • Logician

      #3
      Re: HttpWebRequest - data is truncated?

      On Apr 24, 10:15 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
      Logician <sa...@logician s.comwrote:
      I am looking for some help about the method below which partly works.
      The problem is that the data returned is truncated in that the
      returned data has missing bytes near the beginning of the data. I am
      using VS 2005 and C#.
      >
      The StreamReader is probably buffering data - reading more than 25
      bytes and then saving it in its buffer.
      >
      Instead of using a StreamReader, just use the Stream directly to start
      with. You're interested in the bytes which represent "GIF8", not the
      actual *text* "GIF8", right?
      >
      --
      Jon Skeet - <sk...@pobox.co m>http://www.pobox.com/~skeet  Blog:http://www.msmvps.com/jon.skeet
      World class .NET training in the UK:http://iterativetraining.co.uk
      Yes the 25 bytes was the issue. I saw code at
      http://weblogs.asp.net/guys/archive/...21/414118.aspx which
      helped a lot.

      Comment

      Working...