请教高人:WebClient如何获得有错误的网页,就象IE一样容错?

shyworm 2003-10-29 04:36:01
当网页有错误时,IE浏览器仍然可以获得页面内容;
但WebClient却因异常而什么也得不到,请教高手如何解决呢?
我希望在有错误时,WebClient仍然可以获得一些内容。
...全文
182 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
shyworm 2003-11-09
  • 打赏
  • 举报
回复
怪了,我换别的机器都可以看到足球联赛的内容的。问题究竟在哪里呢?
saucer 2003-11-07
  • 打赏
  • 举报
回复
我看到的是

Server Error in '/WebClient' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review

.....

跟你上面的HTML一模一样

你还是没理解,你的网页有错,服务器传回来这出错的内容,浏览器就直接显示出来了,但.NET因为看到了网页的StatusCode是500,所以它为了提醒你,把出错内容包含在一个WebException,然后告诉你出错了

你可以不理它,你用Socket获取内容就知道了
shyworm 2003-11-07
  • 打赏
  • 举报
回复
你用浏览器访问如下地址试试,结果是正常的网站网页,而不是错误提示。只是在浏览器的状态栏的左边会有一个惊叹号图标和文字“网页上有错误”,但网站的网页内容仍然显示了的。

http://www.interwetten.com/webclient/betting/offer.aspx?type=1&kindofsportid=10&L=EN
saucer 2003-11-07
  • 打赏
  • 举报
回复
我不明白你的问题,这就是浏览器显示的东西啊,你需要什么内容?

你的网页有错,服务器传回来这出错的内容,.NET看到是500,就给你个WebException,但浏览器就直接显示出来了
saucer 2003-11-07
  • 打赏
  • 举报
回复
I am getting this error page in the browser both at work and home
shyworm 2003-11-07
  • 打赏
  • 举报
回复
非常感谢saucer(思归)的帮助,不过返回结果中没有我想要的内容呀。(Console输出如下)

是否跟Encoding enc = Encoding.GetEncoding("GB2312");这句代码有关呢?因为这个页面不是中文网页。但我试过改成Encoding enc = Encoding.GetEncoding("iso-8859-1");也不行。
我是用你给的程序运行测试的。

<html>
<head>
<title>Object reference not set to an instance of an object.</title>
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;c
olor:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-t
op: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top
: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;col
or:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;col
or: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>Server Error in '/WebClient' Application.<hr width=100% si
ze=1 color=silver></H1>

<h2> <i>Object reference not set to an instance of an object.</i> </
h2></span>

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


<b> Description: </b>An unhandled exception occurred during the exec
ution of the current web request. Please review the stack trace for more informa
tion about the error and where it originated in the code.

<br><br>

<b> Exception Details: </b>System.NullReferenceException: Object ref
erence not set to an instance of an object.<br><br>

<b>Source Error:</b> <br><br>

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

An unhandled exception was generated during the execution of the current web req
uest. Information regarding the origin and location of the exception can be iden
tified using the exception stack trace below.</code>

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

<br>

<b>Stack Trace:</b> <br><br>

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

[NullReferenceException: Object reference not set to an instance of an object.]
Interwetten.WebClient.betting.offer.Page_Load(Object sender, EventArgs e) +20
2
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +753
</pre></code>

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

<br>

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

<b>Version Information:</b> Microsoft .NET Framework Version:1.
1.4322.573; ASP.NET Version:1.1.4322.573

</font>

</body>
</html>
<!--
[NullReferenceException]: Object reference not set to an instance of an object.
at Interwetten.WebClient.betting.offer.Page_Load(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain()
[HttpUnhandledException]: Exception of type System.Web.HttpUnhandledException wa
s thrown.
at System.Web.UI.Page.HandleError(Exception e)
at System.Web.UI.Page.ProcessRequestMain()
at System.Web.UI.Page.ProcessRequest()
at System.Web.UI.Page.ProcessRequest(HttpContext context)
at System.Web.CallHandlerExecutionStep.System.Web.HttpApplication+IExecutionS
tep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& compl
etedSynchronously)
-->
Press any key to continue
shyworm 2003-11-07
  • 打赏
  • 举报
回复
奇怪了,我看到的是网站的内容啊,而不是错误提示。这是个足球网站,显示的是联赛列表。IE 5或6都一样,显示如下:

Clear Selection Next

Soccer


Please select one or more of the following bet offers.



England FA Cup (90')


England Premier League


England First Division


Germany Bundesliga


Germany Second League


Germany Third League South


Germany Third League North


Italy Serie A


Italy Serie B


Spain Premier Division


Spain Segunda Division


Scotland Premier League


Scotland First Division


France First Division


France Second Division


Portugal First League


Holland Ehrendivision


Holland Second League


Turkey First League


Greece First League


Austria Bundesliga


Austria First League


Switzerland Cup (90')


Denmark Superleague


Norway Cup Finale Oslo (90´)


Czech Republic First league


Republic Ireland Premier


Belgium First League


Poland First League


Belgium Second League


Major League Soccer (90')


Argentina Apertura


Mexico 1a Liga



Select all Clear Selection Next


saucer 2003-11-03
  • 打赏
  • 举报
回复
if you copy the whole output to a html page, you will see that is exactly what you will get using a browser

When the .NET library receives a 500 error, it creates a WebException, wrap the respsonse inside its stream and throws the exception to your application code
shyworm 2003-11-03
  • 打赏
  • 举报
回复
saucer(思归) 的做法只能获取WebResponse(如下),并没有获取目标网页。

<html>
<head>
<title>Object reference not set to an instance of an object.</title>
...
saucer 2003-10-31
  • 打赏
  • 举报
回复
the exception is thrown by the .NET, you need to catch it, for example:

using System;
using System.Net;
using System.IO;
using System.Text;

class TestClient
{
public static void Main()
{
string sURL = "http://www.interwetten.com/webclient/betting/offer.aspx?type=1&kindofsportid=10&L=EN";
Encoding enc = Encoding.GetEncoding("GB2312");

try
{
WebClient wc = new WebClient();
Console.WriteLine(enc.GetString(wc.DownloadData(sURL)));
}
catch (WebException ex)
{

WebResponse wr = ex.Response;

StreamReader sr= new StreamReader(wr.GetResponseStream(), enc);
string sHtml = sr.ReadToEnd();
Console.WriteLine(sHtml);
sr.Close();
}
}
shyworm 2003-10-31
  • 打赏
  • 举报
回复
gOODiDEA(无语) 说的对,返回的信息确实是:
远程服务器返回错误: (500) 内部服务器错误。

怎么办呢?
shyworm 2003-10-31
  • 打赏
  • 举报
回复
是这样的啊!你试试这个地址:
http://www.interwetten.com/webclient/betting/offer.aspx?type=1&kindofsportid=10&L=EN

IE可以,Webclient却不行。
gOODiDEA 2003-10-30
  • 打赏
  • 举报
回复
如果是500错,还是得不到有用的信息。
saucer 2003-10-30
  • 打赏
  • 举报
回复
are you serious?

>>>当网页有错误时,IE浏览器仍然可以获得页面内容

if it is a server error, what you see on the screen is information from the browser itself, not received from the server

if the page is displayed in the browser but you get scripting errors, then you should always get the content with WebClient or whatever networking tool

do


try
{
//use your webclient here
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
if (ex.InnerException != null)
Console.WriteLine(ex.InnerException.Message);
}

shyworm 2003-10-29
  • 打赏
  • 举报
回复
大哥大姐帮帮忙啊!

111,094

社区成员

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

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

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