Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > HttpWebResponse contains carriage returns, tab characters, etc?

Reply
Thread Tools

HttpWebResponse contains carriage returns, tab characters, etc?

 
 
Dave
Guest
Posts: n/a
 
      07-01-2008
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.
 
Reply With Quote
 
 
 
 
densial
Guest
Posts: n/a
 
      07-01-2008
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.
 
Reply With Quote
 
 
 
 
Anthony Jones
Guest
Posts: n/a
 
      07-01-2008
"Dave" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
nick chan
Guest
Posts: n/a
 
      07-02-2008
do u get same results with System.Net.WebClient? (simpler anyway)

On Jul 2, 1:08*am, Dave <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying text that contains carriage return/line feed rjweytens Java 3 07-30-2004 03:37 AM
Help: Delete a single carriage return in a file, but not a double carriage return? Steve Anderson Perl Misc 3 06-21-2004 12:48 AM
HttpWebResponse/HttpWebRequest problem... Satinderpal Singh ASP .Net 2 06-04-2004 04:40 AM
Carriage Return, Tab Jim Heavey ASP .Net 1 05-13-2004 06:33 AM
Regex problem, match if line contains <a>, unless it also contains <b> James Dyer Perl 5 02-20-2004 12:29 PM



Advertisments