91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ASP.NET獲得新浪天氣預報幾種方式分別是什么

發布時間:2021-10-28 09:24:28 來源:億速云 閱讀:259 作者:柒染 欄目:編程語言

這篇文章將為大家詳細講解有關ASP.NET獲得新浪天氣預報幾種方式分別是什么,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

1.利用新浪提供給的iframe直接嵌入,這種方式非常的簡單,但是卻沒有交互性。代碼如下:

<iframe frameborder="0" src="http://php.weather.sina.com.cn/widget/weather.php"
scrolling="no" width="246" height="360"></iframe>

2.抓取當天的天氣,以指定格式輸出。

涉及的核心代碼如下:

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("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 err)
{
throw new Exception("訪問地址出錯~~~ ");
}

 這里涉及到一個ConvertCode類,它的作用是用于把城市轉換為對應的全國統一的編碼,代碼如下:

using System;
using System.Collections.Generic;
using System.Web;


", thirdWindforceStartIndex);
string ThirdWindforce = Html.Substring(thirdWindforceStartIndex + 3, thirdWindforceEndIndex - thirdWindforceStartIndex - 3);

3.獲取三天以內的天氣,以指定格式輸出。

核心代碼如下:

public static ArrayList GetThreeDayWeather(string City)
{
ArrayList al = new ArrayList();
/*
[0] "今天 北京"              string
[1] "2009-04-17,星期五"     string
[2] "晴轉多云"               string
[3] "12℃"                   string
[4] "25℃"                   string
[5] "2-3級"                  string
[6] "明天 北京"              string
[7] "2009-04-18,星期六"     string
[8] "陰轉陣雨"               string
[9] "11℃"                   string
[10] "21℃"                  string
[11] "2-3級"                 string
[12] "后天 北京"             string
[13] "2009-04-19,星期日"    string
[14] "多云轉陣雨"            string
[15] "9℃"                   string
[16] "20℃"                  string
[17] "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("訪問地址出錯~~~ ");
}

//去除多余代碼,便于分析跟提高效率
int count = Html.Length;
int starIndex = Html.IndexOf("

", 0, count);
int endIndex = Html.IndexOf("

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

try
{
#region 得到今天的天氣

//得到今天的標識跟城市
int firstDayAndCityStartIndex = Html.IndexOf("

", 0);
int firstDayAndCityEndIndex = Html.IndexOf("

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

//得到今天的日期跟星期
int firstDateStartIndex = Html.IndexOf("

", firstDayAndCityEndIndex);
int firstDateEndIndex = Html.IndexOf("

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

//得到今天的天氣
int firstWeatherStartIndex = Html.IndexOf("

", firstDateEndIndex);
int firstWeatherEndIndex = Html.IndexOf(" ", firstWeatherStartIndex + 24);
string FirstWeather = Html.Substring(firstWeatherStartIndex + 24, firstWeatherEndIndex - firstWeatherStartIndex - 24);

//得到今天的溫度

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);

//得到今天的風力
int firstWindforceStartIndex = Html.IndexOf("風力:", firstTemperatureEndIndex);
int firstWindforceEndIndex = Html.IndexOf("

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

if (FirstWindforce.Contains("至"))
{

}
else if (FirstWindforce.Contains("<"))                  //判斷風力是否含有"<"或"≤"字樣將,如果有的話,將其替換為2-
{
string strWindforce = FirstWindforce.Substring(1, FirstWindforce.Length - 2);
int minWindforce = Int32.Parse(strWindforce) - 1;
FirstWindforce = FirstWindforce.Replace("<", minWindforce.ToString() + "-");
}
else if (FirstWindforce.Contains("≤"))
{
string strWindforce = FirstWindforce.Substring(1, FirstWindforce.Length - 2);
int minWindforce = Int32.Parse(strWindforce) - 1;
FirstWindforce = FirstWindforce.Replace("≤", minWindforce.ToString() + "-");
}

#endregion

#region 得到明天的天氣

//得到明天的標識跟城市
int secondDayAndCityStartIndex = Html.IndexOf("

", firstWindforceEndIndex);
int secondDayAndCityEndIndex = Html.IndexOf("

", secondDayAndCityStartIndex);
string secondDayAndCity = Html.Substring(secondDayAndCityStartIndex + 4, secondDayAndCityEndIndex - secondDayAndCityStartIndex - 4);

//得到明天的日期跟星期
int secondDateStartIndex = Html.IndexOf("

", secondDayAndCityEndIndex);
int secondDateEndIndex = Html.IndexOf("

", secondDateStartIndex);
string SecondDate = Html.Substring(secondDateStartIndex + 3, secondDateEndIndex - secondDateStartIndex - 3).Replace(" ", ",");

//得到明天的天氣
int secondWeatherStartIndex = Html.IndexOf("

", secondDateEndIndex);
int secondWeatherEndIndex = Html.IndexOf(" ", secondWeatherStartIndex + 24);
string SecondWeather = Html.Substring(secondWeatherStartIndex + 24, secondWeatherEndIndex - secondWeatherStartIndex - 24);

//得到明天的溫度

int secondTemperatureStartIndex = secondWeatherEndIndex + 1;
int secondTemperatureEndIndex = Html.IndexOf("

", secondTemperatureStartIndex);
string SecondTemperature = Html.Substring(secondTemperatureStartIndex, secondTemperatureEndIndex - secondTemperatureStartIndex);
int int4 = SecondTemperature.IndexOf("℃", 0);
int int5 = SecondTemperature.IndexOf("~", 0);
int int6 = SecondTemperature.IndexOf("℃", int2);
string SecondMinTemperature = SecondTemperature.Substring(int5 + 1, int6 - int5);
string SecondMaxTemperature = SecondTemperature.Substring(0, int5 - int4 + 2);

//得到明天的風力
int secondWindforceStartIndex = Html.IndexOf("風力:", secondTemperatureEndIndex);
int secondWindforceEndIndex = Html.IndexOf("

", secondWindforceStartIndex);
string SecondWindforce = Html.Substring(secondWindforceStartIndex + 3, secondWindforceEndIndex - secondWindforceStartIndex - 3);

if (SecondWindforce.Contains("至"))
{

}
else if (SecondWindforce.Contains("<"))                  //判斷風力是否含有"<"或"≤"字樣將,如果有的話,將其替換為2-
{
string strWindforce = SecondWindforce.Substring(1, FirstWindforce.Length - 2);
int minWindforce = Int32.Parse(strWindforce) - 1;
SecondWindforce = SecondWindforce.Replace("<", minWindforce.ToString() + "-");
}
else if (SecondWindforce.Contains("≤"))
{
string strWindforce = SecondWindforce.Substring(1, SecondWindforce.Length - 2);
int minWindforce = Int32.Parse(strWindforce) - 1;
SecondWindforce = SecondWindforce.Replace("≤", minWindforce.ToString() + "-");
}

#endregion

#region 得到后天的天氣

//得到后天的標識跟城市
int thirdDayAndCityStartIndex = Html.IndexOf("

", secondWindforceEndIndex);
int thirdDayAndCityEndIndex = Html.IndexOf("

", thirdDayAndCityStartIndex);
string thirdDayAndCity = Html.Substring(thirdDayAndCityStartIndex + 4, thirdDayAndCityEndIndex - thirdDayAndCityStartIndex - 4);

//得到后天的日期跟星期
int thirdDateStartIndex = Html.IndexOf("

", thirdDayAndCityEndIndex);
int thirdDateEndIndex = Html.IndexOf("

", thirdDateStartIndex);
string ThirdDate = Html.Substring(thirdDateStartIndex + 3, thirdDateEndIndex - thirdDateStartIndex - 3).Replace(" ", ",");

//得到后天的天氣
int thirdWeatherStartIndex = Html.IndexOf("

", thirdDateEndIndex);
int thirdWeatherEndIndex = Html.IndexOf(" ", thirdWeatherStartIndex + 24);
string ThirdWeather = Html.Substring(thirdWeatherStartIndex + 24, thirdWeatherEndIndex - thirdWeatherStartIndex - 24);

//得到后天的溫度

int thirdTemperatureStartIndex = thirdWeatherEndIndex + 1;
int thirdTemperatureEndIndex = Html.IndexOf("

", thirdTemperatureStartIndex);
string ThirdTemperature = Html.Substring(thirdTemperatureStartIndex, thirdTemperatureEndIndex - thirdTemperatureStartIndex);
int int7 = ThirdTemperature.IndexOf("℃", 0);
int int8 = ThirdTemperature.IndexOf("~", 0);
int int9 = ThirdTemperature.IndexOf("℃", int2);
string ThirdMinTemperature = ThirdTemperature.Substring(int8 + 1, int9 - int8);
string ThirdMaxTemperature = ThirdTemperature.Substring(0, int8 - int7 + 2);

//得到后天的風力
int thirdWindforceStartIndex = Html.IndexOf("風力:", thirdTemperatureEndIndex);
int thirdWindforceEndIndex = Html.IndexOf("


特殊說明,使用第三種方法獲取天氣預報,輸入城市的時候可能會受新浪提供的服務的影響,可能有些城市搜不到,前兩種方法應該是不會受影響的。另外,由于代碼寫的比較急,所以難免代碼的質量就會有些問題,還請大家多多包涵。單純從代碼上看,可能確實沒有什么難度,可是如果在您的工作中如果因為我的代碼為您節省了一些時間,筆者深感欣慰了。

另外,由于我的開發環境是VS2008+sp1,如果您的vs版本較低,不妨把項目文件刪除掉,然后打開您的VS,選擇打開網站,然后定位到本程序的目錄,這樣就可以進行代碼的編譯跟蹤調試了。

再補充一點,有人可能對代碼中出現的:System.Environment.NewLine 這句代碼比較迷糊,因為這個是.NET提供的簡單的換行方法,我推薦大家以后能夠使用這種.net提供給大家的高效、簡便的方法。最關鍵的是它不容易出錯。

關于ASP.NET獲得新浪天氣預報幾種方式分別是什么就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

金堂县| 郑州市| 洛宁县| 隆林| 双流县| 利川市| 梅河口市| 嘉兴市| 张家口市| 关岭| 溧阳市| 灵台县| 林口县| 合山市| 页游| 龙井市| 彭阳县| 金湖县| 广安市| 家居| 民勤县| 吐鲁番市| 晋江市| 宜君县| 蓝田县| 兰州市| 灵武市| 沾益县| 汝州市| 响水县| 若羌县| 武汉市| 高雄市| 威远县| 陈巴尔虎旗| 新余市| 成安县| 马尔康县| 瑞昌市| 台中市| 寿阳县|