Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are several ways for ASP.NET to obtain Sina weather forecast?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail for you about ASP.NET to get Sina weather forecast several ways are what, the article content quality is high, so Xiaobian share for everyone to make a reference, I hope you read this article after the relevant knowledge has a certain understanding.

1. Using iframe provided by Sina to embed directly, this method is very simple, but there is no interactivity. The code is as follows:

2. Capture the weather of the day and output it in the specified format.

The core code involved is as follows:

public static ArrayList GetWeather(string code){/*[0] "Beijing"string[1] "Thunderstorm"string[2] "9℃" string[3] "29℃"string[4] "Less than 3"string*/string html = "";try{HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://weather.sina.com.cn/iframe/weather/" + 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 r){throw new Exception("Access address error ~~ ");}

This involves a ConvertCode class, which is used to convert the city to the corresponding national unified code, the code is as follows:

using System;using System.Collections.Generic;using System.Web;", thirdWindforceStartIndex);string ThirdWindforce = Html.Substring(thirdWindforceStartIndex + 3, thirdWindforceEndIndex - thirdWindforceStartIndex - 3);

3. Get weather within three days, output in specified format.

The core code is as follows:

public static ArrayList GetThreeDayWeather(string City){ArrayList al = new ArrayList();/*[0] "Today Beijing" string[1] "Friday, April 17, 2009" string[2] "Clear to cloudy" string[3] "12℃" string[4] "25℃" string[5] "Level 2-3" string[6] "Tomorrow Beijing" string[7] "Saturday, April 18, 2009" string[8] "overcast to showers" string[9] "11℃" string[10] "21℃" string[11] "Level 2-3" string[12] "The day after tomorrow Beijing" string[13] "Sunday, April 19, 2009" string[14] "Cloudy to showers" string[15] "9℃" string[16] "20℃" string[17] "Level 2-3" string */string Html = ""; //ASCIIEncoding encoding = new ASCIIEncoding();string postData = string.Format("city=" + City);byte[] data = encoding.GetBytes(postData);

try

{

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://php.weather.sina.com.cn/search.php? city=" + System.Web.HttpContext.Current.Server.UrlEncode(City) + "&f=1&dpc=1");

request.Method = "Get";

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("Error accessing address ~~~ ");

}

//Remove redundant code for analysis and efficiency

int count = Html.Length;

int starIndex = Html.IndexOf("

", 0, count);

int endIndex = Html.IndexOf("

", 0);

Html = Html.Substring(starIndex, endIndex - starIndex);

try

{

#region Get today's weather

//Get today's logo and city

int firstDayAndCityStartIndex = Html.IndexOf("

", 0);

int firstDayAndCityEndIndex = Html.IndexOf("

", 0);

string FirstDayAndCity = Html.Substring(firstDayAndCityStartIndex + 4, firstDayAndCityEndIndex - firstDayAndCityStartIndex - 4);

//get today's date and week

int firstDateStartIndex = Html.IndexOf("

", firstDayAndCityEndIndex);

int firstDateEndIndex = Html.IndexOf("

", firstDayAndCityEndIndex);

string FirstDate = Html.Substring(firstDateStartIndex + 3, firstDateEndIndex - firstDateStartIndex - 3).Replace(" ", ",");

//Get today's weather

int firstWeatherStartIndex = Html.IndexOf("

", firstDateEndIndex);

int firstWeatherEndIndex = Html.IndexOf(" ", firstWeatherStartIndex + 24);

string FirstWeather = Html.Substring(firstWeatherStartIndex + 24, firstWeatherEndIndex - firstWeatherStartIndex - 24);

//get today's temperature

int firstTemperatureStartIndex = firstWeatherEndIndex + 1;

int firstTemperatureEndIndex = Html.IndexOf("

", firstTemperatureStartIndex);

string FirstTemperature = Html.Substring(firstTemperatureStartIndex, firstTemperatureEndIndex - firstTemperatureStartIndex);

int int1 = FirstTemperature.IndexOf("℃", 0);

int int2 = FirstTemperature.IndexOf("~", 0);

int int3 = FirstTemperature.IndexOf("℃", int2);

string FirstMinTemperature = FirstTemperature.Substring(int2 + 1, int3 - int2);

string FirstMaxTemperature = FirstTemperature.Substring(0, int2 - int1 + 2);

//get today's wind

int firstWindforceStartIndex = Html.IndexOf("Windforce: ", firstTemperatureEndIndex);

int firstWindforceEndIndex = Html.IndexOf("

", firstWindforceStartIndex);

string FirstWindforce = Html.Substring(firstWindforceStartIndex + 3, firstWindforceEndIndex - firstWindforceStartIndex - 3);

if (FirstWindforce.Contains("to"))

{

}

else if (FirstWindforce.Contains("

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report