本文實例講述了asp.net實現根據城市獲取天氣預報的方法。分享給大家供大家參考,具體如下:
public void GetWetheaerByCity(string cityName) { string city = cityName; string mycity = HttpUtility.UrlEncode(city, System.Text.Encoding.GetEncoding("GB2312")); System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://www.sina.com/index.php?city=" + cityName); //HttpWebRequest 對象 獲取GetResponse 轉換成HttpWebResponse System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse; //通過HttpWebResponse response.GetResponseStream()獲取輸出流 Stream str = response.GetResponseStream(); StreamReader reader = new StreamReader(str, System.Text.Encoding.GetEncoding("GB2312")); string weathhtml = reader.ReadToEnd(); str.Close(); reader.Close(); }
希望本文所述對大家asp.net程序設計有所幫助。