C#winform读取asp网页

zwb521 2009-05-06 07:27:02
我是这么写的
hwr = (HttpWebRequest)HttpWebRequest.Create(url);
hwre = (HttpWebResponse)hwr.GetResponse();
Stream s = hwre.GetResponseStream();
StreamReader sr = new StreamReader(s, encoding))
string strBody = sr.ReadToEnd();

读取到
。。。。。。。。。。。。。。。
<table width="760" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td><table width="330" border="0" cellspacing="0" cellpadding="0"><tr>
<td> 每页<span class="STYLE1">15</span>条 总共<span class="STYLE1">0</span>条 当前<span class="STYLE1">1</span>/<span class="STYLE1">1</span>页</td>
</tr></table>
。。。。。。。。。。。。。。。


这个网页是有数据的,可是加载不出来,是什么原因啊
...全文
329 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanxiaolin520 2009-05-08
  • 打赏
  • 举报
回复
sorry
昨天在网上找了下gb2312编码的读取方法,也没找到。
你自己在研究下吧。
企业通用软件 2009-05-07
  • 打赏
  • 举报
回复
这是我用的一个获取页面代码的函数,可以参考一下:
而且还能自动获取到网页的编码,很好用的

/// <summary>
/// 获取指定的网页代码
/// </summary>
/// <param name="url">Url</param>
/// <param name="encoding">编码,未知直接传入null</param>
/// <returns></returns>
public static string GetHtml(string url, Encoding encoding)
{
string strBackHtml = "";
HttpWebRequest myRequest;
HttpWebResponse myResponse;
StreamReader myReader = null;
Stream myStream = null;


myRequest = (HttpWebRequest)HttpWebRequest.Create(url);
myRequest.Timeout = 60000;
myRequest.AllowAutoRedirect = true;

try
{
myResponse = (HttpWebResponse)myRequest.GetResponse();
}
catch (Exception ex)
{
return "Error:" + ex.Message;
}

if (myResponse.StatusCode == HttpStatusCode.OK)
{
myStream = myResponse.GetResponseStream();

if (encoding == null)
{
# region 未知编码

try
{
MemoryStream msTemp = new MemoryStream();
int len = 0;
byte[] buff = new byte[512];

while ((len = myStream.Read(buff, 0, 512)) > 0)
{
msTemp.Write(buff, 0, len);
}
myResponse.Close();

if (msTemp.Length > 0)
{
msTemp.Seek(0, SeekOrigin.Begin);
byte[] PageBytes = new byte[msTemp.Length];
msTemp.Read(PageBytes, 0, PageBytes.Length);

msTemp.Seek(0, SeekOrigin.Begin);
int DetLen = 0;
byte[] DetectBuff = new byte[4096];
CharsetListener listener = new CharsetListener();
UniversalDetector Det = new UniversalDetector(null);
while ((DetLen = msTemp.Read(DetectBuff, 0, DetectBuff.Length)) > 0 && !Det.IsDone())
{
Det.HandleData(DetectBuff, 0, DetectBuff.Length);
}
Det.DataEnd();
if (Det.GetDetectedCharset() != null)
{
//字符编码
//CharSetBox.Text = "OK! CharSet=" + Det.GetDetectedCharset();
//网页代码
strBackHtml = System.Text.Encoding.GetEncoding(Det.GetDetectedCharset()).GetString(PageBytes);
}
}
}
catch (Exception ex)
{
return "Error:" + ex.Message;
}
finally
{
//释放资源
if (myResponse != null)
{
myResponse.Close();
myResponse = null;
}
if (myReader != null)
{
myReader.Close();
}
if (myRequest != null)
{
myRequest = null;
}
}

# endregion
}
else
{
# region 已知编码

try
{
if (myResponse.StatusCode == HttpStatusCode.OK && myResponse.ContentLength < 1024 * 1024)
{
if (myResponse.ContentEncoding != null && myResponse.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
{
myReader = new StreamReader(new System.IO.Compression.GZipStream(myResponse.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress), encoding);
}
else
{
myReader = new StreamReader(myResponse.GetResponseStream(), encoding);
}
strBackHtml = myReader.ReadToEnd();
}
}
catch (Exception ex)
{
return "Error:" + ex.Message;
}
finally
{
//释放资源
if (myResponse != null)
{
myResponse.Close();
myResponse = null;
}
if (myReader != null)
{
myReader.Close();
}
if (myRequest != null)
{
myRequest = null;
}
}

# endregion
}
}

return strBackHtml;
}

public class CharsetListener : ICharsetListener
{
public string Charset;
public void Report(string charset)
{
this.Charset = charset;
}
}
zwb521 2009-05-07
  • 打赏
  • 举报
回复
是啊 呵呵
可能做了保护
whyabc 2009-05-07
  • 打赏
  • 举报
回复
up
tanxiaolin520 2009-05-07
  • 打赏
  • 举报
回复
好像是读不到..晕.
看看在回贴.
bobbycj 2009-05-07
  • 打赏
  • 举报
回复
zwb521 2009-05-07
  • 打赏
  • 举报
回复
garfield21
试了,谢谢,但不好用
获取效果依旧
zwb521 2009-05-06
  • 打赏
  • 举报
回复
你读取这个网页试试http://www.q-vod.com.cn/index.asp?class=喜剧片&page=1
想要的数据加载不出来
<title>『Qvod资源网』</title>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<meta name="keywords" content="Qvod资源网--快播资源--免费Qvod电影资源"/>
<meta name="description" content="Qvod资源网,是目前国内最大最全的qvod资源网,每天更新大量qvod资源,包括电影、动漫、综艺、电视剧等等。免费提供给所有qvod站长使用!" />
<LINK REL="SHORTCUTICON" HREF="FAVICON.ICO">
<LINK REL="BOOKMARK" HREF="FAVICON.ICO">
<LINK href="images/css.css" type=text/css rel=stylesheet>
</HEAD>

<body>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="320" rowspan="4"><img src="images/qvod_logo.gif" /></td>
<td width=650" height="27" align="center" class="STYLE5"><strong>欢迎使用『www.q-vod.com.cn』QVOD资源网,本站永久提供免费Q播资源无信任域!</strong></td>
</tr>
<tr>
<td height="27" align="center">
站长QQ群①:75942995   QQ群②:72734380(已满)   QQ群③:81392236
</td>
</tr>
<TR><form method=post action=index.asp><td><div align=center>
<LABEL>影片名模糊搜索 <INPUT class=input01 id=keyword name=keyword> </LABEL>
<input type="submit" name="Submit" value="搜 索" />
<LABEL><INPUT type=radio CHECKED value=title
name=action>片名 <INPUT type=radio value=name name=action>主演</LABEL></div></td></TR>
<tr></form>
<td height="27" align="center"><a href="javascript:window.external.addFavorite('http://www.q-vod.com.cn/','『Qvod资源网』');">收藏本站</a> | <span class="STYLE5">使用本站资源请做好本站友情连接</span></td>
</tr>
</table>
<table width="100%" height="30" border="0" cellpadding="0" cellspacing="0" background="bg.gif" bgcolor="#18A30C">
<tr>
<td align="center"><span class="bai STYLE7"><a href="" class="bai">全部影片</a> 
<a href=index.asp?class=喜剧片 class="bai">喜剧</a> 
<a href=index.asp?class=动作片 class="bai">动作</a> 
<a href=index.asp?class=爱情片 class="bai">爱情</a> 
<a href=index.asp?class=恐怖片 class="bai">恐怖</a> 
<a href=index.asp?class=战争片 class="bai">战争</a> 
<a href=index.asp?class=教育片 class="bai">教育</a> 
<a href=index.asp?class=综艺片 class="bai">综艺</a> 
<a href=index.asp?class=科幻片 class="bai">科幻</a> 
<a href=index.asp?class=动画片 class="bai">动画</a> 
<a href=index.asp?class=其他类 class="bai">其他</a> 
<a href=index.asp?class=连续剧&area=港台 class="bai">港台剧</a> 
<a href=index.asp?class=连续剧&area=大陆 class="bai">大陆剧</a> 
<a href=index.asp?class=连续剧&area=日韩 class="bai">日韩剧</a> 
<a href=index.asp?class=连续剧&area=欧美 class="bai">欧美剧</a> 
<a href=index.asp?class=连载区 class="bai">连载区</a>
</span></td>
</tr>
</table>
<table width="760" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td width="495" height="20" align="center" bgcolor="#F3F3F3"><strong>影片名称</strong></td>
<td width="79" align="center" bgcolor="#F3F3F3"><strong>出产地区</strong></td>
<td width="79" align="center" bgcolor="#F3F3F3"><strong>获取地址</strong></td>
<td width="79" align="center" bgcolor="#F3F3F3"><strong>影片类型</strong></td>
<td colspan=2 align="center" width=170 bgcolor="#F3F3F3"><strong>更新时间</strong></td>
</tr>

<tr><td colspan=6 height=360 align=center>暂时无此相关记录</tr></td>
<tr><td> </td><tr></table>

<table width="760" border="0" align="center" cellpadding="0" cellspacing="0"><tr><td><table width="330" border="0" cellspacing="0" cellpadding="0"><tr>
<td> 每页<span class="STYLE1">15</span>部 总共<span class="STYLE1">0</span>部 当前<span class="STYLE1">1</span>/<span class="STYLE1">1</span>页</td>
</tr></table>
</td><td><table border="0" width="330" align="center"><tr><td width="20%" align="center"></td><td width="20%" align="center"></td><td width="60%" align="center" colspan="2">
<a href=http://www.q-vod.com.cn/index.asp?class=%E5%96%9C%E5%89%A7%E7%89%87&vsi=1&page=1>首页</a> 

上一页</a> 
下一页</a> 
<a href=http://www.q-vod.com.cn/index.asp?class=%E5%96%9C%E5%89%A7%E7%89%87&vsi=1&page=1>
尾页</a> 
</td></tr>
</table></td>
</tr>
</table>
</div>
<br><br>
<table width="760" border="0" align="center">
<a href=http://www.qvod.com target='_blank'>快播Qvod官网</a>  
<a href=http://www.hongxx.com target='_blank'>红叉叉影院</a>  
<a href=http://www.12345hao.cn target='_blank'>神州影院</a>  
<a href=http://www.1gvod.com target='_blank'>1G高清电影</a>  
<a href=http://www.snv8.cn target='_blank'>淑女影吧</a>  
<a href=http://www.lolowo.cn target='_blank'>乐乐窝影吧</a>  
<a href=http://www.vod0738.cn target='_blank'>娄底影院</a>  
<a href=http://WWW.17k17k.com target='_blank'>17K电影网</a>  
<a href=http://www.youku3344.cn target='_blank'>优酷高清</a>

<TABLE width=100% border=0 align=center cellPadding=0 cellSpacing=0 bgColor=#ffffff>
<TBODY>
<TR><TD bgColor=#66C55B height=1></TD></TR>
<TR><TD bgColor=#18A208 height=2></TD></TR>
<TR><TD bgColor=#edeae3 height=3></TD></TR></TBODY></TABLE>
<table width="960" border="0" align="center" cellpadding="0" cellspacing="0">
<br>
<TR>
<TD colSpan=2 height=26><div align=center><font color=9C9A9C>
声明:本站仅为网友提供交流.qsed文件的信息平台,所有资源的实际文件并不在本站服务器上,而是网友自发共享的,更不保证文字描述和实际内容相符。 
<script type="text/javascript" src="http://js.tongji.cn.yahoo.com/826579/ystat.js"></script><noscript><a href="http://tongji.cn.yahoo.com"><img src="http://img.tongji.cn.yahoo.com/826579/ystat.gif"/></a></noscript><br>
   除了.qsed文件并没有任何文件在本站服务器上。如果您认为有.qsed文件或描述信息侵犯了您的版权,请及时举报,我们将尽快为您处理。  苏ICP备08113206号
</font></div></TD>
</TR>
</table>
<script language="javascript" src="http://u5.778669.com/qqshow.php?id=86595&h=130&w=200"></script>
</BODY></HTML>

tanxiaolin520 2009-05-06
  • 打赏
  • 举报
回复
那只接载就OK拉,现在什么问题?
你现在是载不到指定的字符?你先载长点看看,就OK拉。
zwb521 2009-05-06
  • 打赏
  • 举报
回复
这个网站就没注册用户啊,就是没有任何限制访问的网页
所以不存在账号cookie的问题吧
tanxiaolin520 2009-05-06
  • 打赏
  • 举报
回复
你是说这个网页不需读COOKIE值 ,直接用帐号和密码是不是?
你载的时候先全部看看能不能出来。或者你把你的全码弄来看看。
zwb521 2009-05-06
  • 打赏
  • 举报
回复
这个页不需要啊,完全用用户名密码
tanxiaolin520 2009-05-06
  • 打赏
  • 举报
回复
你的网页是不是需要帐号和密码的?
如果需要的话,就必须先加载哦.
给你个之前我做的例子:
namespace GetUrl
{
public class GetHtmlUrl
{
private static string CookVlaue = FileOpenCookies();

/// <summary>
/// 根据网址返回对应HTML代码
/// </summary>
/// <param name="UriAddress"></param>
/// <returns></returns>
public static string GetUrlTxt(string UriAddress)
{
HttpWebRequest HttpWreq = (HttpWebRequest)WebRequest.Create(UriAddress); //初始化打开网页实例
//HttpWreq.ContentType = "application/x-www-form-urlencoded";
//HttpWreq.ContentType = "application/x-shockwave-flash";
Cookie mycookie = new Cookie();
mycookie.Name = "T3E";
if (CookVlaue == " ")
{
return "没有读取到用户登陆信息";
}
mycookie.Value = CookVlaue;
mycookie.Domain = "网址";
//给mycookie值
CookieContainer mycookiecontainer = new CookieContainer();
mycookiecontainer.Add(mycookie);

HttpWreq.CookieContainer = mycookiecontainer;

HttpWreq.Method = "GET";
HttpWreq.AllowAutoRedirect = true;
HttpWreq.KeepAlive = true;
HttpWebResponse HttpWresp = (HttpWebResponse)HttpWreq.GetResponse();

//Console.WriteLine(HttpWresp.ProtocolVersion.ToString());
//返回版本号
Encoding gb2312 = Encoding.GetEncoding("GB2312");
Encoding Utf8 = Encoding.GetEncoding("UTF-8");
Stream myStream = HttpWresp.GetResponseStream();
StreamReader myReader = new StreamReader(myStream, Utf8);
string var = myReader.ReadToEnd();

myReader.Close(); //关闭
HttpWresp.Close();

return var; //返回取回的值
}

/// <summary>
/// 读取用户登陆信息,如果成功返回登陆信息,否则返回 "没有读取到用户登陆信息"
/// </summary>
/// <returns></returns>
private static string FileOpenCookies()
{
string RetCookValue = ""; //存放读出来的COOKIE值
try
{
string CookiePath = Environment.GetFolderPath(Environment.SpecialFolder.Cookies);
string username = Environment.UserName;
CookiePath += "\\" + username + "@s2.travian[2].txt";
if (!File.Exists(CookiePath))
{
CookiePath = CookiePath.Replace("[2]", "[1]");
}
string[] cookieValue = File.ReadAllLines(CookiePath, Encoding.ASCII);
if (cookieValue.Length == 9)
{
RetCookValue = cookieValue[1];
RetCookValue = Uri.UnescapeDataString(RetCookValue);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return " ";
}
return RetCookValue;
}
}
}
蓝海D鱼 2009-05-06
  • 打赏
  • 举报
回复
up

110,526

社区成员

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

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

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