请大家帮忙解决一个天气预报程序错误的问题!错误:StartIndex 不能小于 0。参数名: startIndex

jeall 2003-08-19 11:53:49
ASP.NET的实时天气及24小时天气预报(C#)
修改其中的url获得其他城市的天气情况
如广州为:
http://weather.yahoo.com/forecast/CHXX0037_c.html
注意仅适用于获得yahoo上的天气预报

错误提示:
StartIndex 不能小于 0。参数名: startIndex
源文件: c:\inetpub\wwwroot\importent\getweather.aspx.cs 行: 85

GetWeather.aspx
-----------------------------------

<%@ Page language="c#" Codebehind="GetWeather.aspx.cs" AutoEventWireup="false" Inherits="test.GetWeather" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>GetWeather</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="GetWeather" method="post" runat="server">
<FONT face="宋体">
<P>
<asp:Label id="lblWeather" runat="server">Weather</asp:Label></P>
<P>
<asp:Button id="btnGet" runat="server" Text="Get Weather"></asp:Button></P>
<P>
<asp:Label id="Weather2" runat="server">24小时天气</asp:Label></P>
<P>
<asp:Button id="btnGet2" runat="server" Text="天气预报"></asp:Button></P>
</FONT>
</form>
</body>
</HTML>



getWeather.aspx.cs
---------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;

namespace test
{

public class GetWeather : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblWeather;
protected System.Web.UI.WebControls.Label Weather2;
protected System.Web.UI.WebControls.Button btnGet2;
protected System.Web.UI.WebControls.Button btnGet;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnGet.Click += new System.EventHandler(this.btnGet_Click);
this.btnGet2.Click += new System.EventHandler(this.btnGet2_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnGet_Click(object sender, System.EventArgs e)
{

WebRequest wreq=WebRequest.Create("http://weather.yahoo.com/forecast/CHXX0037_c.html");

HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();

string HTML ="";
Stream s=wresp.GetResponseStream();

StreamReader objReader = new StreamReader(s);

string sLine = "";
int i = 0;

while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML += sLine;
}

String temp= "";
int start,stop;

start = HTML.IndexOf("<!-- CURCON-->",0,HTML.Length);

stop = HTML.IndexOf("<!-- END CURCON-->",0,HTML.Length);

temp = HTML.Substring(start, stop - start);
start = temp.IndexOf("<b>");
stop = temp.IndexOf("</b>");

string degree = temp.Substring(start+3,stop - start -3);

start = temp.IndexOf("<img");
stop = temp.IndexOf("</td>",start);

string img = temp.Substring(start,stop - start);
lblWeather.Text = degree + "<br>" + img;

}

private void btnGet2_Click(object sender, System.EventArgs e)
{
WebRequest wreq=WebRequest.Create("http://cn.weather.yahoo.com/CHXX/CHXX0037/index_c.html");

HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();

string HTML ="";
Stream s=wresp.GetResponseStream();

StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));


string sLine = "";
int i = 0;

while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML += sLine;
}

String temp= "";
int start,stop;

start = HTML.IndexOf("<table border=0 cellpadding=2 cellspacing=1 bgcolor=9999cc width=\"85%\">",0,HTML.Length);

stop = HTML.IndexOf("</table>",start)+8;


temp = HTML.Substring(start, stop - start);
Weather2.Text = temp;
}
}
}

...全文
220 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xixigongzhu 2003-08-20
  • 打赏
  • 举报
回复
你也可以改成:如果不存在(-1),返回空。
xixigongzhu 2003-08-20
  • 打赏
  • 举报
回复
如果返回的页面不存在"<!-- CURCON-->",那么IndexOf返回-1,再用Substring时,就报错了。可以这样改一下:
1.start = HTML.IndexOf("<!-- CURCON-->",0,HTML.Length);
stop = HTML.IndexOf("<!-- END CURCON-->",0,HTML.Length);
if (start == -1) start = 0;
if (stop == -1) stop = HTML.Length;
temp = HTML.Substring(start, stop - start);

2.start = temp.IndexOf("<b>");
stop = temp.IndexOf("</b>");
if (start == -1) start = 0;
if (stop == -1) stop = temp.Length;
string degree = temp.Substring(start+3,stop - start -3);

3.start = temp.IndexOf("<img");
if (start == -1) start = 0;
stop = temp.IndexOf("</td>",start);
if (stop == -1) stop = temp.Length;
string img = temp.Substring(start,stop - start);
starfire21 2003-08-20
  • 打赏
  • 举报
回复
WeatherCtrl.ascx页面
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="WeatherCtrl.ascx.cs" Inherits="Weather.WeatherCtrl" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<FONT face="宋体" size="8">
<TABLE borderColor="#cc9966" cellSpacing="0" cellPadding="0" width="100%" border="0">
<TR>
<TD style="WIDTH: 71px"><font size="2">时间:</font></TD>
<TD><asp:label id="Labeltime" runat="server">Label</asp:label></TD>
</TR>
<TR>
<TD style="WIDTH: 71px"><font size="2">最低气温:</font></TD>
<TD>
<P><asp:label id="Labelbottom" runat="server">Label</asp:label></P>
</TD>
</TR>
<TR>
<TD style="WIDTH: 71px"><font size="2">最高气温:</font></TD>
<TD>
<P><asp:label id="Labeltop" runat="server">Label</asp:label></P>
</TD>
</TR>
<TR>
<TD style="WIDTH: 71px"><font size="2">上午:</font></TD>
<TD>
<asp:Label id="Labelmorning" runat="server">Label</asp:Label></TD>
</TR>
<TR>
<TD style="WIDTH: 71px"><font size="2">下午:</font></TD>
<TD>
<asp:Label id="Labelafternoon" runat="server">Label</asp:Label></TD>
</TR>
</TABLE>
</FONT>

WeatherCtrl.cs文件
namespace Weather
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;
using System.Net;
using System.IO;

/// <summary>
/// WeatherCtrl 的摘要说明。
/// </summary>
public abstract class WeatherCtrl : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label Labeltime;
protected System.Web.UI.WebControls.Label Labeltop;
protected System.Web.UI.WebControls.Label Labelbottom;
protected System.Web.UI.WebControls.Label Labelmorning;
protected System.Web.UI.WebControls.Label Labelafternoon;
public string City;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面

}

private string GetLastTdContent(string tr)
{
int start,stop,start2,i=0;
string tempstr;
start=0;
start2=0;
while(i!=3)
{
start2=start;
start=tr.IndexOf("<td",start+3);
i++;
}

stop=tr.IndexOf("</td>",start2);
tempstr=tr.Substring(start2,stop-start2);

start=tempstr.IndexOf(">",0,tempstr.Length-1);
return tempstr.Substring(start+1);

}
private string GetImgContent(string img)
{
int start,stop;
string tempstr;
start=0;
start=img.IndexOf("symbole/",0);
stop=img.IndexOf(".gif",0);
tempstr=img.Substring(start+8,stop-start-4);

//start=tempstr.IndexOf(">",0,tempstr.Length-1);
return tempstr;

}
private string GetALTContent(string img)
{
int start,stop;
string tempstr;
start=0;
start=img.IndexOf("ALT=",0);
stop=img.IndexOf("width",0);
tempstr=img.Substring(start+5,stop-start-7);

//start=tempstr.IndexOf(">",0,tempstr.Length-1);
return tempstr;

}
public void GetWeather(string url)
{
WebRequest wreq=WebRequest.Create(url);
//wreq.Timeout=10;

HttpWebResponse wresp=(HttpWebResponse)wreq.GetResponse();

string HTML ="";
Stream s=wresp.GetResponseStream();

StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));

string sLine = "";
int i = 0;

while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML += sLine;
}

String temp= "";
int start,stop;
string line1,line2,line3,line4,line5;
string td1,td2,td3,td4,td5,img4,img5,alt4,alt5;

start = HTML.IndexOf("<table width=\"415\" border=\"1\" cellspacing=\"0\" cellpadding=\"1\" BGCOLOR=\"#ffffff\">",0,HTML.Length);

stop = HTML.IndexOf("<tr> <td align=\"left\" nowrap height=\"10\" colspan=\"4\"></td> </tr> ",start)+8;


temp = HTML.Substring(start, stop - start);
temp=temp +"</table>";
HTML=temp;
//get line1
start=HTML.IndexOf("<tr",0,HTML.Length);
stop=HTML.IndexOf("</tr>",start)+5;
line1=HTML.Substring(start,stop-start);

td1=GetLastTdContent(line1);
start=HTML.IndexOf("<tr",stop,HTML.Length -stop);
stop=HTML.IndexOf("</tr>",start)+5;
line2=HTML.Substring(start,stop-start);

td2=GetLastTdContent(line2);
start=HTML.IndexOf("<tr",stop,HTML.Length -stop);
stop=HTML.IndexOf("</tr>",start)+5;
line3=HTML.Substring(start,stop-start);

td3=GetLastTdContent(line3);
start=HTML.IndexOf("<tr",stop,HTML.Length -stop);
stop=HTML.IndexOf("</tr>",start)+5;
line4=HTML.Substring(start,stop-start);
img4=GetLastTdContent(line4);
td4=GetImgContent(img4);
alt4=GetALTContent(img4);
td4="<img src=\"images/weather/"+td4+"\" ALT=\""+alt4+"\">";

start=HTML.IndexOf("<tr",stop,HTML.Length -stop);
stop=HTML.IndexOf("</tr>",start)+5;
line5=HTML.Substring(start,stop-start);

img5=GetLastTdContent(line5);
td5 =GetImgContent(img5);
alt5=GetALTContent(img5);
td5="<img src=\"images/weather/"+td5+"\" ALT=\""+alt5+"\">";
//this.Labelafternoon.Text="<table>" +line1 + line2 +"</table>";
this.Labeltime.Text=td1;
this.Labelbottom.Text=td2;
this.Labeltop.Text=td3;
this.Labelmorning.Text=td4;
this.Labelafternoon.Text=td5;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// 设计器支持所需的方法 - 不要使用
/// 代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
xixigongzhu 2003-08-20
  • 打赏
  • 举报
回复
多试几个网页,找出这些网页的相同点,针对相同点处理出数据。
jeall 2003-08-20
  • 打赏
  • 举报
回复
我测试通过了,不过结果是不是应该跟预想的不一样吧!
结果显示了一个"Hello,Guest"和一个"Weather.com"的图片,是不是应该显示广州的天气?
其实,我看这个例子,是想能够活血活用,比如改一改样式、显示内容等,或者只显示一句话天气预报,变成自己的天气预报。
在这方面你还有什么心得吗?我觉得源程序是在搜索Yahoo网页的一些字符串信息,然后显示我们需要的内容,这方面我一无所知,想跟你交流交流,谢谢!

110,499

社区成员

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

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

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