Reputation: 1599
I am using a NameValueCollection
to post to a website. The response array when converted to string contains "\r\n
". I need pure html so I can use the string to display the content on a page. Here is the code:
byte[] responseArray = myWebClient.UploadValues(SubmitURL, myNameValueCollection);
string s = Encoding.ASCII.GetString(responseArray);
s contains something like this:
\r\n \r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\r\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n\r\n\r\n<head>\r\n<title>Transaction Code Search Results</title>\r\n<link href=\"searchreference.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n\r\n</head>\r\n\r\n<body topmargin=\"10\" leftmargin=\"10\">\r\n<table border=\"0\" width=\"100%\">\r\n <tr>\r\n <td width=\"70\" rowspan=\"2\" align=\"left\"><img src=\"images/WFLogo_62.gif\" alt=\"WF logo\" width=\"62\" height=\"62\" /></td>\r\n <td height=\"40\"> </td>\r\n <td rowspan=\"2\"><form method=\"post\" action=\"trancodesearch3.asp\">\r\n <div align=\"right\">\r\n <input type=\"submit\" name=\"Search again\"\r\n value=\"Search again\" />\r\n </div>\r\n </form></td>\r\n </tr>\r\n <tr>\r\n <td valign=\"bottom\"><h1>Transaction Code Search Results</h1></td>\r\n </tr>\r\n</table>\r\n\r\n<hr size=\"1\" class=\"redline\" />\r\n\r\n<table width=\"100%\" border=\"0\" align=\"
left\" cellpadding=\"6\"
Upvotes: 1
Views: 20912
Reputation: 1757
You can do like this
var htmlString = System.Text.Encoding.Default.GetString(response); // reponse is byte[]
Upvotes: 0
Reputation: 1038890
You are looking the value of the s
variable in Visual Studio's debugger. That's why you are seeing all those \r\n
symbols. In reality those are just new lines contained in the string and your code of converting the byte array to a string using the Encoding.ASCII.GetString
method is correct.
One minor adjustment you could adopt is the encoding itself. You could use the encoding that was calculated by the WebClient using the HTTP response headers:
byte[] responseArray = myWebClient.UploadValues(SubmitURL, myNameValueCollection);
string s = myWebClient.Encoding.GetString(responseArray);
So for example if the remote website supports a different encoding than ASCII, this would take it into account and you won't get data loss during the conversion (you know the ???? symbols where you expect to get some accented characters for example).
Upvotes: 6