HttpWebResponse contains carriage returns, tab characters, etc?

Discussion in 'ASP .Net' started by Dave, Jul 1, 2008.

  1. Dave

    Dave Guest

    I'm trying to download a webpage by using the HttpWebRequest. It returns the
    html source, however, it contains "\r\n", "\t" etc throughout the text. Is
    there a way to return the same HTML as when I navigate to the url in the
    browser and do a "View Source"? Or do I have to manually strip these out?
    The full code is below where I'm posting the necessary data to simulate a
    form post.

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
    req.Method = "POST";
    req.UserAgent = "Mozilla/4.0+";
    req.ContentType = "application/x-www-form-urlencoded";

    System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
    byte[] PostBuffer = encoding.GetBytes(PostData);
    req.ContentLength = PostBuffer.Length;
    Stream stm = req.GetRequestStream();
    stm.Write(PostBuffer, 0, PostBuffer.Length);
    stm.Close();

    // Get the response.
    resp = req.GetResponse() as HttpWebResponse;
    sr = new StreamReader(resp.GetResponseStream());

    string result = sr.ReadToEnd(); <--returns the source but with carriage
    returns etc.
    Dave, Jul 1, 2008
    #1
    1. Advertising

  2. Dave

    densial Guest

    I would expect your source to contain CR's and Tabs, this is probably
    the way it's being sent. if you see something else in a "view source"
    I suspect that's a problem with the browser, not with the request.

    so yes, you have to manually strip these out.
    densial, Jul 1, 2008
    #2
    1. Advertising

  3. "Dave" <> wrote in message
    news:...
    > I'm trying to download a webpage by using the HttpWebRequest. It returns

    the
    > html source, however, it contains "\r\n", "\t" etc throughout the text.

    Is
    > there a way to return the same HTML as when I navigate to the url in the
    > browser and do a "View Source"? Or do I have to manually strip these out?
    > The full code is below where I'm posting the necessary data to simulate a
    > form post.
    >
    > HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
    > req.Method = "POST";
    > req.UserAgent = "Mozilla/4.0+";
    > req.ContentType = "application/x-www-form-urlencoded";
    >
    > System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
    > byte[] PostBuffer = encoding.GetBytes(PostData);
    > req.ContentLength = PostBuffer.Length;
    > Stream stm = req.GetRequestStream();
    > stm.Write(PostBuffer, 0, PostBuffer.Length);
    > stm.Close();
    >
    > // Get the response.
    > resp = req.GetResponse() as HttpWebResponse;
    > sr = new StreamReader(resp.GetResponseStream());
    >
    > string result = sr.ReadToEnd(); <--returns the source but with carriage
    > returns etc.


    I have a suspision that you believe the string actually contains \r\n and \t
    rather than the controls control code equivalents and that you believe this
    because that's what the debugger is showing you. However the debugger shows
    you the string in an escaped form that is valid as string literal in C#.

    OTH something really bizare is happening.

    --
    Anthony Jones - MVP ASP/ASP.NET
    Anthony Jones, Jul 1, 2008
    #3
  4. Dave

    nick chan Guest

    do u get same results with System.Net.WebClient? (simpler anyway)

    On Jul 2, 1:08 am, Dave <> wrote:
    > I'm trying to download a webpage by using the HttpWebRequest.  It returns the
    > html source, however, it contains "\r\n", "\t" etc throughout the text.  Is
    > there a way to return the same HTML as when I navigate to the url in the
    > browser and do a "View Source"? Or do I have to manually strip these out?  
    > The full code is below where I'm posting the necessary data to simulate a
    > form post.
    >
    > HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
    > req.Method = "POST";
    > req.UserAgent = "Mozilla/4.0+";
    > req.ContentType = "application/x-www-form-urlencoded";
    >
    > System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
    > byte[] PostBuffer = encoding.GetBytes(PostData);
    > req.ContentLength = PostBuffer.Length;
    > Stream stm = req.GetRequestStream();
    > stm.Write(PostBuffer, 0, PostBuffer.Length);
    > stm.Close();
    >
    > // Get the response.
    > resp = req.GetResponse() as HttpWebResponse;
    > sr = new StreamReader(resp.GetResponseStream());
    >
    > string result = sr.ReadToEnd(); <--returns the source but with carriage
    > returns etc.
    nick chan, Jul 2, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joerg Jooss

    Re: HttpWebResponse and special characters

    Joerg Jooss, Aug 7, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    653
    Joerg Jooss
    Aug 7, 2004
  2. ThePants
    Replies:
    9
    Views:
    6,164
    Joerg Jooss
    Aug 31, 2006
  3. Bazza Formez
    Replies:
    2
    Views:
    916
    Bazza Formez
    Aug 6, 2007
  4. Kevin Walzer

    Re: PIL (etc etc etc) on OS X

    Kevin Walzer, Aug 1, 2008, in forum: Python
    Replies:
    4
    Views:
    369
    Fredrik Lundh
    Aug 13, 2008
  5. Steve Anderson
    Replies:
    3
    Views:
    237
    Steve Anderson
    Jun 21, 2004
Loading...

Share This Page