HttpWebResponse字符集有关问题
HttpWebResponse字符集问题很简单的一段代码,但是返回的字符集好像有问题,不知道什么原因,谁能给解释下,谢
HttpWebResponse字符集问题
很简单的一段代码,但是返回的字符集好像有问题,不知道什么原因,谁能给解释下,谢谢。
C# codepublic static void Main(){ // Create a request for the URL. WebRequest request = WebRequest.Create(@"http://www.163.com"); // If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials; // Get the response. HttpWebResponse response = (HttpWebResponse)request.GetResponse();//此处查看response的CharacterSet属性,为CharacterSet = "ISO-8859-1",而直接查看网页的源文件为<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> }
很是奇怪,我用的是VS2010
[解决办法]取得 <meta http-equiv= "content-type " content= "text/html;charset=gb2312 ">
http://topic.csdn.net/u/20080821/07/f127d7d5-f885-4c86-a06f-e539c95cb216.html
[解决办法]HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Console.WriteLine("The encoding method used is: " + myHttpWebResponse.ContentEncoding);
Console.WriteLine("The character set used is :" + myHttpWebResponse.CharacterSet);