HttpWebRequest获取网页,中文字符为何变成乱码?

zhou_she 2005-09-15 12:23:29
本程序在Microsoft Visual Web Developer 2005 Bata2中文版下调试:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim mr As HttpWebRequest
Dim mt As HttpWebResponse
Dim sr As StreamReader
Dim li As String
mr = CType(WebRequest.Create("http://www.163.com/"), HttpWebRequest)
mt = CType(mr.GetResponse(), HttpWebResponse)
sr = New StreamReader(mt.GetResponseStream())
li = sr.ReadLine()
Do While Not li Is Nothing
li = HttpUtility.HtmlEncode(li)
If li.Length <> 0 Then
Response.Write(li & "<BR>")
End If
li = sr.ReadLine()
Loop
End Sub
得到如下页面:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>????</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="content-language" content="gb2312" />
<style type=text/css>
<!--
html {min-width:750px;}
body {text-align:center; margin:0px;}
body > div {text-align:center; margin-right:auto; margin-left:auto;}
form, input, select {margin:0px; padding:0px;}
body, div {font-size:12px; font-family:????;}
img,a img{border:0; margin:0; padding:0;}
input,select{font-size:12px;vertical-align:middle}
/*=====urs??½??=====*/
#UrsLogin {border:1px solid #ccc; background:#EBEBEB; padding:2px; width:744px!important; width /**/:750px; height:22px!important; height /**/:28px;}
#userName,#password{width:73px}
#userName{color:#aaa}
#loginSelect{width:90px}
/*=====????Logo & Ƶ??????=====*/
<div class="Text1">
<a href="http://ent.163.com/star/">????</a> <a href="http://ent.163.com/special/c/00031JN6/chaonv2005.html">"??Ů"????</a> <a href="http://ent.163.com/05/0914/09/1TJN616H00031H2F.html">?????˷??DZ????Ľ?ֹ</a><br /><a href="http://bbs.netease.com/game/index.php?forumcode=273">?ڲ????룡???ס????ơ?18?????</a><br /><a href="http://app.game.163.com/caicai/">??ϷIQ???ƴ???Ҳ???Ŀ¡???Ƴ???</a><br />
</div>
<div class="Text2">
<a href="http://education.163.com/">????</a> <a href="http://education.163.com/">?????߿??Ծ??????</a> <a href="http://education.163.com/kaoyan/">2006???м???</a></a><br /><a </h3>
</div>
<!-- ????logo3 Start -->
<div id="Logo300">
<iframe src="http://adclient.163.com/html.ng/site=netease&affiliate=homepage&cat=homepage&type=logo300120&location=3" width="300" height="120" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
</div>
<!-- ????logo3 End -->
<!-- ???׶?̬ Start-->
<div id="Netease">
<h2 class="Title2">
<div>???׶?̬ ?? </div><a href="http://design.163.com/newsletter/all/">???׼?Ѷ</a>
</h2>
<div class="OneC">
??<a href="http://corp.163.com/corpnews/editor/050907/050907_2698.html">??2??"????ȫ??ժ?Ǵ??ж?"?????ܾ???????????</a><br />
??<a href="http://gb.corp.163.com/corpnews/editor/050901/050901_2694.html">?°?POPO???????黶?????ɹ?ͨ</a><br />
??<a href="http://gb.corp.163.com/corpnews/editor/050803/050803_2692.html">???׷???2005??ڶ????ȲƱ?????????2930????Ԫ</a><br />
??<a href="http://corp.163.com/corpnews/index.html">????</a>...
</div>
</div>
<!-- ???׶?̬ End-->
</div>
</div>
<<…………………………
</script>
<noscript>
<img src="//secure-cn.imrworldwide.com/cgi-bin/m?ci=cn-netease&cg=0" alt="">
</noscript>
<!-- END NNR Site Census V5.1 -->
</body>
</html>
以上内容只要是中文的地方都是乱码,请问应该如何解决?谢谢!(但在获取www.liall.com这个页面时却不会出现任何异常现象)


...全文
570 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
cuike519 2005-09-15
  • 打赏
  • 举报
回复
在new StreamReader的时候指定编码。如果页面的编码方式是中文则指定GB2312,如果是UTF-8则指定UTF-8。

比如(C#):
StreamReader sr= new StreamReader (tm.GetResponseStream(), Encoding.GetEncoding("GB2312") );

62,040

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧