100分求解远程读取网站源代码问题

aideng 2009-04-02 09:08:47
我要读“魔兽英雄榜”网站的源代码,网址是:
http://cn.wowarmory.com/character-sheet.xml?r=%E8%AF%95%E7%82%BC%E4%B9%8B%E7%8E%AF&n=%E6%9C%88%E4%B8%8B
其中参数"r=%E8%AF%95%E7%82%BC%E4%B9%8B%E7%8E%AF"是我输入的服务器名,解码后为:"r=试练之环";“n=%E6%9C%88%E4%B8%8B”是玩家名,解码后为"n=月下"
现在打开这个网面后我查看源代码为xml格式:
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="/layout/character-sheet.xsl"?><page globalSearch="1" lang="zh_cn" requestUrl="/character-sheet.xml">
<characterInfo>
<character battleGroup="Battle Group 18" charUrl="r=%E8%AF%95%E7%82%BC%E4%B9%8B%E7%8E%AF&n=%E6%9C%88%E4%B8%8B" class="圣骑士" classId="2" faction="部落" factionId="1" gender="女性" genderId="1" guildName="燃烧末日黎明" guildUrl="r=%E8%AF%95%E7%82%BC%E4%B9%8B%E7%8E%AF&n=%E7%87%83%E7%83%A7%E6%9C%AB%E6%97%A5%E9%BB%8E%E6%98%8E&p=1" lastModified="2009年4月1日" level="70" name="月下" points="1150" prefix="" race="血精灵" raceId="10" realm="试炼之环" suffix="">
...............略
</page>
这个网页的源代码这就略了,你可以打开网页自己查看,
但是我用程序读的时后却是html格式:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
....略
</body>
</html>
我的程序是这样写的:
//根据Url地址生成静态页保持
protected void Button2_Click(object sender, EventArgs e)
{
Encoding code = Encoding.GetEncoding("utf-8");
StreamReader sr = null;
StreamWriter sw = null;
string str = null;
//读取远程路径
WebRequest temp = WebRequest.Create("这里是上面的网址");
WebResponse myTemp = temp.GetResponse();
sr = new StreamReader(myTemp.GetResponseStream(), code);
//读取
try
{
sr = new StreamReader(myTemp.GetResponseStream(), code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xml";
//写入
try
{
sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sw.Close();
Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已经生成,保存在htm文件夹下!");
}
}


我现在就是想把这个网站的xml格式读取出来,不要html格式的,请问怎么写程序,
另外为什么网页打开后我查看源代码是xml格式的,而我用程序读的时候却是html格式的呢?

...全文
137 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
aideng 2009-04-02
  • 打赏
  • 举报
回复
问题已解决代码如下:
public string GetPage()
{
StreamReader reader = null;
HttpWebRequest obj = (HttpWebRequest)WebRequest.Create("http://cn.wowarmory.com/character-sheet.xml?r=%E8%AF%95%E7%82%BC%E4%B9%8B%E7%8E%AF&n=%E6%9C%88%E4%B8%8B");
obj.UserAgent = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9\r\n";
obj.Headers.Add("Accept-Language","zh-CN\r\n");
HttpWebResponse response = (HttpWebResponse)obj.GetResponse();
if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)
{
reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
string html = reader.ReadToEnd();
return html;
}
return string.Empty;
}
ls3697264 2009-04-02
  • 打赏
  • 举报
回复
JF
aideng 2009-04-02
  • 打赏
  • 举报
回复
这个是我改的:
public string GetPage(string url)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
StreamReader reader = null;
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "cn.wowarmory.com";
request.Timeout = 20000;
request.AllowAutoRedirect = false;
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)
{
reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
string html = reader.ReadToEnd();
return html;
}
}
catch
{
}
finally
{
if (response != null)
{
response.Close();
response = null;
}
if (reader != null)
reader.Close();
if (request != null)
request = null;
}
return string.Empty;
}
但是返回的还是html的
aideng 2009-04-02
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 jietuan 的回复:]
使用Httpwebrequest不过是xml还是html,如果是标准的xml,那你就可以用xmldocument操作了。
[/Quote]
我把这个网页打开查看源文件是标准的xml,
而我用程序读取是html
mjjzg 2009-04-02
  • 打赏
  • 举报
回复
UP
jietuan 2009-04-02
  • 打赏
  • 举报
回复
使用Httpwebrequest不过是xml还是html,如果是标准的xml,那你就可以用xmldocument操作了。
aideng 2009-04-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 songhuan 的回复:]
http://tech.it168.com/d/2008-06-12/200806120733969.shtml

我的代码太大了 你参考这个吧
[/Quote]
我读出来的是html,
与打开这个网页再查看它的源代码不一样,查看源代码是标准xml格式的
songhuan 2009-04-02
  • 打赏
  • 举报
回复
http://tech.it168.com/d/2008-06-12/200806120733969.shtml

我的代码太大了 你参考这个吧
aideng 2009-04-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wenblue7 的回复:]
顶了
[/Quote]
顶没用啊,有没有什么办法!!!!
wenblue7 2009-04-02
  • 打赏
  • 举报
回复
顶了
aideng 2009-04-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 songhuan 的回复:]
我使用HttpWebRequest已经测试成功了
[/Quote]

读的是xml格式吗?
把代码给我发一份好吗?
qq:179746171
Email:zhufengming@126.com
songhuan 2009-04-02
  • 打赏
  • 举报
回复
我使用HttpWebRequest已经测试成功了
songhuan 2009-04-02
  • 打赏
  • 举报
回复
使用HttpWebRequest,不要使用webclient.
因为HttpWebRequest默认接收所有类型的服务器返回,webclient应该是只接收html的,会自动转换

因为我的代码太庞大了 就不发给你了

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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