我用的代码是以下的文章(网上的很多文章都是这个)
http://www.lukin.cn/program/dot_net/2006-2-21/k17008.htm

解决方案 »

  1.   

    谢谢您了,报错如下
    Server Error in '/' Application.
    --------------------------------------------------------------------------------Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: startIndex 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: startIndexSource Error: 
    Line 102: int start,stop;   
    Line 103: start = HTML.IndexOf("<table border=0 cellpadding=2 cellspacing=1 bgcolor=9999cc width=\"85%\">",0,HTML.Length);   
    Line 104: stop = HTML.IndexOf("</table>",start)+8;   
    Line 105:
    Line 106: temp = HTML.Substring(start, stop - start);   
     Source File: c:\inetpub\wwwroot\weatherp\index.aspx.cs    Line: 104 Stack Trace: 
    [ArgumentOutOfRangeException: Index was out of range.  Must be non-negative and less than the size of the collection.
    Parameter name: startIndex]
       System.Globalization.CompareInfo.IndexOfString(Void* pSortingTable, Int32 win32LCID, String source, String value, Int32 startIndex, Int32 count, Int32 options) +0
       System.Globalization.CompareInfo.IndexOf(String source, String value, Int32 startIndex) +45
       System.String.IndexOf(String value, Int32 startIndex) +67
       weatherP.WebForm1.btnGet2_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\weatherp\index.aspx.cs:104
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
       System.Web.UI.Page.ProcessRequestMain() +1277 
      

  2.   

    代码如下
    <%@ 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>
      

  3.   

    后台的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;   
    }   
    }   
    }
      

  4.   

    你调试 断点 看看
    start = HTML.IndexOf("<table border=0 cellpadding=2 cellspacing=1 bgcolor=9999cc width=\"85%\">",0,HTML.Length);
    这里的start值是多少
    应该是这个值 的问题
      

  5.   


    hertcloud(·£孙子兵法£·),谢谢您,请看以下网址,错误好像不是您说的
    http://202.181.126.4/index.aspx
      

  6.   

    http://weather.yahoo.com/forecast/CHXX0037_c.html
    这个地址输出的html代码中 根本没有<!-- CURCON-->这里开始就是错的start = HTML.IndexOf("<!-- CURCON-->", 0, HTML.Length);应该是yahoo应该修改了 http://weather.yahoo.com/forecast/CHXX0037_c.html
    输出的html代码
    你可以 重新看看html然后 
    利用上面的原理 重新获取
      

  7.   

    可能是文章出的太久了,所以……,您知道哪里有天气预报的RSS或是别的什么可能利用吗?
      

  8.   

    http://weather.265.com/
    楼猪分析这个网站的天气吧,蛮全的。