HttpWebRequest return a WebResponse with strange numbers
i have a strange problem. When i get the response of a httpwebresquest, some numbers appears in the middle of the html text.
For example:
< input type="hidden" name="productid" value="7220701403
841
89620" >
That 841 is a number that should not appear, and as such are more every few rows. Even at the beginning:c04
< html >
< head >
So, it's impossible to parse the html.
UTF8Encoding enco开发者_运维知识库ding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(postDataString);
request.ContentLength = bytes.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.SendChunked = false;
using (Stream writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
response = (HttpWebResponse)request.GetResponse();
Stream remoteStream = response.GetResponseStream();
byte[] buffer = new byte[65536];
int bytesRead = 0;
do
{
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
UTF8Encoding enc = new UTF8Encoding();
responseString += enc.GetString(buffer);
} while (bytesRead > 0);
remoteStream.Close();
The html text is in variable responseString.
Thanks for any ideas and suggestions.
First, You assume that the server uses UTF8. You also don't consider how many bytes you read (bytesRead = remoteStream.Read(buffer, 0, buffer.Length)
)and just convert to whole buffer to string (enc.GetString(buffer)
). It should be something like enc.GetString(buffer,0,bytesRead)
PS: A bytesRead>=0
check could also be needed
精彩评论