所在位置:首页 > 苍天 > 正文

ASP.NET实现天气预报

发布时间:2023-10-17 16:00:53 来源:网络 作者:

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Data.SqlClient;

using System.Net;

using System.IO;

using System.Collections;

/// <summary>

/陪轮// Weather 的摘码皮要说明

/// </summary>

public class Weather

{

public Weather()

{

//

// TODO: 在此处添加构造函数逻辑

//

}

public static string ConvertCodeByCity(string City)

{

string Code = ;

switch (City)

{

case 北京:

Code = 110100;

break;

default:

break;

}

return Code;

}

public static ArrayList GetWeather(string code)

{

/*

[0] 北京 string

[1] 雷阵雨 string

[2] 9℃ string

[3] 29℃string

[4] 小于3级string

*/

string html = ;

try

{

HttpWebRequest request = (HttpWebRequest)WebRequest.Create( + code + _w.html );

request.Method = Get;

//request.Timeout = 1;

request.ContentType = 迟乱差application/x-www-form-urlencoded ;

WebResponse response = request.GetResponse();

Stream s = response.GetResponseStream();

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

html = sr.ReadToEnd();

s.Close();

sr.Close();

}

catch (Exception err)

{

throw new Exception(访问地址出错~~~ );

}

int count = html.Length;

int starIndex = html.IndexOf(<table , 0, count);

int endIndex = html.IndexOf(</table>, starIndex, count - starIndex);

html = html.Substring(starIndex, endIndex - starIndex + 8);

//得到城市

int cityStartIndex = html.IndexOf(<b>, 0, html.Length);

int cityEndIndex = html.IndexOf(</b>, 0, html.Length);

string City = html.Substring(cityStartIndex + 3, cityEndIndex - cityStartIndex - 3);

//得到天气

int weatherStartIndex = html.IndexOf(<b>, cityEndIndex);

int weatherEndIndex = html.IndexOf(</b>, weatherStartIndex);

string Weather = html.Substring(weatherStartIndex + 3, weatherEndIndex - weatherStartIndex - 3);

//得到温度

int temperatureStartIndex = html.IndexOf(<b, weatherEndIndex);

int temperatureEndIndex = html.IndexOf(</b>, weatherEndIndex + 3);

string Temperature = html.Substring(temperatureStartIndex + 21, temperatureEndIndex - temperatureStartIndex - 21);

int int1 = Temperature.IndexOf(℃, 0);

int int2 = Temperature.IndexOf(~, 0);

int int3 = Temperature.IndexOf(℃, int2);

string MinTemperature = Temperature.Substring(int2 + 1, int3 - int2);

string MaxTemperature = Temperature.Substring(0, int2 - int1 + 2);

//得到风力

int windforceStartIndex = html.IndexOf(风力:, temperatureEndIndex);

int windforceEndIndex = html.IndexOf(<br>, windforceStartIndex);

string Windforce = html.Substring(windforceStartIndex + 3, windforceEndIndex - windforceStartIndex - 3);

if (Windforce.Contains(小于) && (!Windforce.Contains(等于))) //判断风力是否含有小于或小于等于字样将,如果有的话,将其替换为2-

{

//Windforce = Windforce.Replace(小于, 2-);

string strWindforce = Windforce.Substring(2, Windforce.Length - 3);

int minWindforce = Int32.Parse(strWindforce) - 1;

Windforce = Windforce.Replace(小于, minWindforce.ToString() + -);

}

else if (Windforce.Contains(小于等于))

{

string strWindforce = Windforce.Substring(4, Windforce.Length - 5);

int minWindforce = Int32.Parse(strWindforce) - 1;

Windforce = Windforce.Replace(小于等于, minWindforce.ToString() + -);

}

ArrayList al = new ArrayList();

al.Add(City);

al.Add(Weather);

al.Add(MinTemperature);

al.Add(MaxTemperature);

al.Add(Windforce);

return al;

}

}

<%@ Page Language=败举C# AutoEventWireup=true CodeFile=Tianqi.aspx.cs Inherits=common_Tianqi %>

<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN >

<html xmlns=>

<head runat=server>

<title></title>芹迅

</head>

<body style=width:98%;height=98%;>

<form id=form1察首碧 runat=server style=width:98%px;height=98%px;>

<div style=width:98%;height=98%;>

<iframe name=weather_inc src= width="200px" height="600px" frameborder=0 marginwidth=0 marginheight=0 scrolling=no ></iframe>

</div>

</form>

</body>

</html>

后台代码为空 创建一个新的aspx页面 将以上代码复制就好了

用webserver

  • 热门资讯
  • 最新资讯
  • 手游排行榜
  • 手游新品榜