获取页面源代码,怎么老是报“远程服务器返回错误: (500) 内部服务器错误”?

Pitt 2008-12-08 10:34:11
        /// <summary>
/// 通过URL执行WEN后获取页面内容
/// </summary>
/// <param name="url"></param>
public static string ExecutePage(string urlstr)
{
string content = "";

if (urlstr.IndexOf("http://", StringComparison.OrdinalIgnoreCase) != 0 && urlstr.IndexOf("/") == 0)
{
urlstr = "http://" + HttpContext.Current.Request.Url.Host + urlstr;
}
else if (urlstr.IndexOf("http://", StringComparison.OrdinalIgnoreCase) != 0)
{
urlstr = "http://" + urlstr;
}

System.Net.WebResponse wResp = null;
System.IO.Stream respStream = null;
System.IO.StreamReader reader = null;
try
{
Uri url = new Uri(urlstr);
System.Net.WebRequest wReq = System.Net.WebRequest.Create(url);
wResp = wReq.GetResponse();
respStream = wResp.GetResponseStream();
reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("utf-8"));
content = reader.ReadToEnd();
}
catch (System.Net.WebException ex)
{
//return ex.Source + ":" + urlstr;
throw;
}
finally
{
if (reader!= null)
reader.Close();
if (respStream != null)
respStream.Close();
if (wResp != null)
wResp.Close();

}
return content;
}


调用ExecutePage("http://www.163.com") 正常。

调用ExecutePage("http://localhost") 正常。

调用ExecutePage("http://localhost/ReviewBox/ReviewArticle.aspx?id=1") 报错。

http://localhost/ReviewBox/ReviewArticle.aspx?id=1 正常显示.


...全文
780 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
kitsmiler 2009-06-05
  • 打赏
  • 举报
回复
被抓取的页面按照你参数的方式肯定报错了,你可以调试一下,直接用字符串的形式访问,看报不报错。我就是这样解决的这个问题的。
rubylily_2009 2009-05-21
  • 打赏
  • 举报
回复
遇到同样问题,求答案
zhnzzy 2008-12-09
  • 打赏
  • 举报
回复
断点试试呀,应该是你URL重写那里出了点问题
yefengzhixia 2008-12-09
  • 打赏
  • 举报
回复
恩 顶了 !! 虽然我不知道为什么。。。。呵呵呵 欢迎加入c#新群 63242231 共同学习
Pitt 2008-12-09
  • 打赏
  • 举报
回复
参数(HttpRequest request )

string sUserAgent = request.UserAgent;是NULL

有什么办法?
Pitt 2008-12-09
  • 打赏
  • 举报
回复
ReviewArticle.aspx 页面有个FCKedit 就有NULL,怎么解决了?
Pitt 2008-12-09
  • 打赏
  • 举报
回复
总算找到原因了,ReviewArticle.aspx 页面有个FCKedit




<html>
<head>
<title>未将对象引用设置到对象的实例。</title>
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:"Lucida Console";font-size: .9em}
.marker {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
</style>
</head>

<body bgcolor="white">

<span><H1>“/”应用程序中的服务器错误。<hr width=100% size=1 color=silver></H1>

<h2> <i>未将对象引用设置到对象的实例。</i> </h2></span>

<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">

<b> 说明: </b>执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

<br><br>

<b> 异常详细信息: </b>System.NullReferenceException: 未将对象引用设置到对象的实例。<br><br>

<b>源错误:</b> <br><br>

<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code>

执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。</code>

</td>
</tr>
</table>

<br>

<b>堆栈跟踪:</b> <br><br>

<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>

[NullReferenceException: 未将对象引用设置到对象的实例。]
FredCK.FCKeditorV2.FCKeditor.IsCompatibleBrowser(HttpRequest request) in C:\Documents and Settings\xsasa\桌面\FCKeditor.Net_2.6.3\FCKeditor.cs:494
FredCK.FCKeditorV2.FCKeditor.IsCompatibleBrowser() in C:\Documents and Settings\xsasa\桌面\FCKeditor.Net_2.6.3\FCKeditor.cs:476
FredCK.FCKeditorV2.FCKeditor.CheckBrowserCompatibility() in C:\Documents and Settings\xsasa\桌面\FCKeditor.Net_2.6.3\FCKeditor.cs:466
FredCK.FCKeditorV2.FCKeditor.OnPreRender(EventArgs e) in C:\Documents and Settings\xsasa\桌面\FCKeditor.Net_2.6.3\FCKeditor.cs:400
System.Web.UI.Control.PreRenderRecursiveInternal() +80
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Control.PreRenderRecursiveInternal() +171
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842
</pre></code>

</td>
</tr>
</table>

<br>

<hr width=100% size=1 color=silver>

<b>版本信息:</b> Microsoft .NET Framework 版本:2.0.50727.3053; ASP.NET 版本:2.0.50727.3053

</font>

</body>
</html>
<!--
[NullReferenceException]: 未将对象引用设置到对象的实例。
在 FredCK.FCKeditorV2.FCKeditor.IsCompatibleBrowser(HttpRequest request) 位置 C:\Documents and Settings\xsasa\桌面\FCKeditor.Net_2.6.3\FCKeditor.cs:行号 494
在 FredCK.FCKeditorV2.FCKeditor.IsCompatibleBrowser() 位置 C:\Documents and Settings\xsasa\桌面\FCKeditor.Net_2.6.3\FCKeditor.cs:行号 476
在 FredCK.FCKeditorV2.FCKeditor.CheckBrowserCompatibility() 位置 C:\Documents and Settings\xsasa\桌面\FCKeditor.Net_2.6.3\FCKeditor.cs:行号 466
在 FredCK.FCKeditorV2.FCKeditor.OnPreRender(EventArgs e) 位置 C:\Documents and Settings\xsasa\桌面\FCKeditor.Net_2.6.3\FCKeditor.cs:行号 400
在 System.Web.UI.Control.PreRenderRecursiveInternal()
在 System.Web.UI.Control.PreRenderRecursiveInternal()
在 System.Web.UI.Control.PreRenderRecursiveInternal()
在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
[HttpUnhandledException]: 引发类型为“System.Web.HttpUnhandledException”的异常。
在 System.Web.UI.Page.HandleError(Exception e)
在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
在 System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
在 System.Web.UI.Page.ProcessRequest()
在 System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
在 System.Web.UI.Page.ProcessRequest(HttpContext context)
在 ASP.reviewbox_reviewarticle_aspx.ProcessRequest(HttpContext context) 位置 c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\7fbc70ed\63e50afc\App_Web_pfnvaqdh.0.cs:行号 0
在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
[HttpException]: Unhandled HttpException while processing NeatUpload child request
在 Brettle.Web.NeatUpload.UploadHttpModule.Application_BeginRequest(Object sender, EventArgs e)
在 System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
在 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
-->
qinhl99 2008-12-09
  • 打赏
  • 举报
回复
或者参考下:
http://topic.csdn.net/t/20020924/14/1049079.html
Pitt 2008-12-09
  • 打赏
  • 举报
回复
忘了说后来我把ID换成articleNO了
qinhl99 2008-12-09
  • 打赏
  • 举报
回复
改成下面测试下:

System.Net.HttpWebResponse wResp = null;
System.IO.Stream respStream = null;
System.IO.StreamReader reader = null;
try
{
Uri url = new Uri(urlstr);
System.Net.WebRequest wReq = System.Net.WebRequest.Create(url);
wResp = (HttpWebResponse)wReq.GetResponse();
respStream = wResp.GetResponseStream();
reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("utf-8"));
content = reader.ReadToEnd();
}
catch (System.Net.WebException ex)
{
//return ex.Source + ":" + urlstr;
throw;
}
Pitt 2008-12-09
  • 打赏
  • 举报
回复
        protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string artNO = this.Request["articleNO"];
if (artNO == null || artNO == "")
{
return;
//这里我就退出去了,不知这样可以吗?
}
modelart = bllart.GetModel(artNO);
if (modelart == null)
{
return;
}
}
}
Pitt 2008-12-09
  • 打赏
  • 举报
回复
http://localhost/ReviewBox/ReviewArticle.aspx
也是允许的。
qinhl99 2008-12-09
  • 打赏
  • 举报
回复
带url参数的不能像你这样直接用!
Pitt 2008-12-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 sq_zhuyi 的回复:]
如果http://localhost/ReviewBox/ReviewArticle.aspx?id=1可以正常显示,就可以正常抓取
再检查
[/Quote]
完全正常
Pitt 2008-12-09
  • 打赏
  • 举报
回复
用到this.Server.Execute却很好。但是怎么获取到源码了,总不能先存在txt在读取吧....
路人乙e 2008-12-09
  • 打赏
  • 举报
回复
如果http://localhost/ReviewBox/ReviewArticle.aspx?id=1可以正常显示,就可以正常抓取
再检查
Pitt 2008-12-09
  • 打赏
  • 举报
回复
wResp = wReq.GetResponse();开始报null

girlhappy 2008-12-08
  • 打赏
  • 举报
回复
Uri url = new Uri(urlstr);
System.Net.WebRequest wReq = System.Net.WebRequest.Create(url);

你看下你的url地址是什麼!
Pitt 2008-12-08
  • 打赏
  • 举报
回复
去掉?id=1也后用 http://localhost/ReviewBox/ReviewArticle.aspx 也是可以直接访问的。
调用ExecutePage("http://localhost/ReviewBox/ReviewArticle.aspx");同样问题。
空心兜兜 2008-12-08
  • 打赏
  • 举报
回复
我觉得是不是这个id=1的问题

加载更多回复(6)

62,269

社区成员

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

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

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

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