473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

HttpWebResponse contains carriage returns, tab characters, etc?

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.
Jul 1 '08 #1
3 2454
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.
Jul 1 '08 #2
"Dave" <Da**@discussions.microsoft.comwrote in message
news:FF**********************************@microsof t.com...
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
Jul 1 '08 #3
do u get same results with System.Net.WebClient? (simpler anyway)

On Jul 2, 1:08*am, Dave <D...@discussions.microsoft.comwrote:
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.
Jul 2 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shiperton Henethe | last post by:
Hi Anyone know how to do a MAILTO: such that the "BODY=" parameter contains carriage returns ? e.g. <A HREF="mailto:brochure@webwindows.co.uk?subject=Brochure Request&amp;body=Line1 blah blah...
8
by: Steven | last post by:
i need to force a carriage return with a textarea field at X number of characters. anybody know how to do this? tks
4
by: Josh | last post by:
Hi, I'm using System.Data.DataSet.ReadXml to convert some xml from a webservice to a DataSet. The xml looks like: <?xml version="1.0"...
2
by: Andrew Chanter | last post by:
I have a VBA function that returns a string including "vbcr" (VB Carriage Return) to seperate a list into multiple rows, eg Item1 & vbcr & Item2 & vbcr & Item3 This works as planned in the...
12
by: Nimmy | last post by:
Hi, I have a data file and I want to remove Carriage returns. Any one has any C code/program which does this? I am working on Windows XP machine.....I don't have access to UNIX machine. But I...
2
by: Matt Mercer | last post by:
Hi all, I am having a frustration problem, and I have read about 25 newsgroup postings that do not have a satisfying answer :) The problem appears to be common where carriage returns are lost...
8
by: TheDude5B | last post by:
Hi, I have some data which is stored in my MySQL database as TEXT. when the data is entered in, it has some carriage returns in it, and this can be seen when querying the data using MySQL Query...
2
by: Bazza Formez | last post by:
I have a bound field in a DetailsView control that displays free form description type data from my SQL database table (typical data is a couple of paragraphs of written product description being...
7
by: newbtemple | last post by:
Hey all. I'm having a bugger of a time trying to remove carriage returns. I have a large text file with a bunch of little squares. I'm guessing they're carriage returns from what i've googled. They...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.