⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webform1.aspx.cs

📁 这是.net2005学习不可缺少的教程
💻 CS
字号:
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 WebApplication1
{
	/// <summary>
	/// Summary description for WebForm1.
	/// </summary>
	public partial class WebForm1 : System.Web.UI.Page
	{
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				writeWeatherInfo(ddlcity.SelectedValue);
				writeWeather(ddlcity.SelectedValue);
			}
		}	

		#region getWeatherByCity 通过城市过虑
		public string getWeatherByCity(string city)
		{
			string temp = null;
			try
			{
                string strURL = "http://php.weather.sina.com.cn/search.php";
				HttpWebRequest request= (HttpWebRequest)WebRequest.Create(strURL);
                //使用 WebRequest.Create 方法初始化 HttpWebRequest 的一个新实例。
                //如果 URI 的方案是 http:// 或 https://,则 Create 将返回 HttpWebRequest 实例。
				request.Method="POST"; //Post请求方式
				request.ContentType="application/x-www-form-urlencoded"; //内容类型
				string paraUrlCoded = System.Web.HttpUtility.UrlEncode("city"); //参数经过URL编码
				paraUrlCoded = paraUrlCoded + "=" + System.Web.HttpUtility.UrlEncode(city, System.Text.Encoding.GetEncoding("GB2312"));
				byte[] payload = System.Text.Encoding.GetEncoding("GB2312").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节 
				request.ContentLength = payload.Length; //设置请求的ContentLength
				Stream writer = request.GetRequestStream(); //获得请求流
				writer.Write(payload,0,payload.Length); //将请求参数写入流
				writer.Close(); //关闭请求流
				HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //获得响应流
				Stream s= response.GetResponseStream();
				StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
				string HTML = "";
				string sLine = "";
				int i = 0; 
				while (sLine!=null) 
				{
					i++;
					sLine = objReader.ReadLine();
					if (sLine!=null) 
						HTML += sLine;
				} 
				HTML = HTML.Replace("&lt;","<");
				HTML = HTML.Replace("&gt;",">");
				int start,stop;
				//start = HTML.IndexOf("<img src=\"http://image2.sina.com.cn/dy/weather/images/figure/",0,HTML.Length);
				start = HTML.IndexOf("<table border=0 cellpadding=0 cellspacing=0 style=\"margin:5px;\">",0,HTML.Length);
				stop = HTML.IndexOf("<td background=http://image2.sina.com.cn/dy/weather/images",start); 
				temp = HTML.Substring(start, stop - start);
			}
			catch (Exception x)
			{
			}
			return temp;
		}

		#endregion

		#region writeWeatherInfo 显示处理后的信息
		private void writeWeatherInfo(string city)
		{
			int start,stop;
			string weather1,weather2,wea;
			string wea_city = getWeatherByCity(city);

			wea_city = wea_city.Replace(" ","");

			start = wea_city.IndexOf("<b>",0,wea_city.Length);
			stop = wea_city.IndexOf("</b>", start);
			weather1 = wea_city.Substring(start, stop-start).Trim() + "          ";
			weather1 = weather1.Substring(3,8).Trim();
   
			start = wea_city.IndexOf("<tdstyle=\"font-size:40px;font-family:TimesNewRoman;font-weight:bold;\">",0,wea_city.Length);
			stop = wea_city.IndexOf("℃",start) + 40;
			weather2 = wea_city.Substring(start, stop-start);
			weather2 = weather2.Substring(stop-start-42,40).Trim();
			weather2 = weather2.Replace("\t","");

			start = wea_city.IndexOf("<fontcolor=#183888><b>", 0, wea_city.Length);
			stop = wea_city.IndexOf("</b></font>",start);
			wea = wea_city.Substring(start,stop-start);
			wea = wea.Substring(22,wea.Length-22) + "kbrk";
			wea = wea.Replace("\t", "");
			wea = wea.Replace(">", "k");
			wea = wea.Replace("<", "k");
			wea = wea.Replace("kbrk", "k");
			string [] wall = null;
			char[] seperator = {'k'};
			wall = wea.Split(seperator);

			
			l_city.Text = "[城市]:" + city;//城市
			l_wea.Text = "[天气]:" + weather1;//天气
			l_sky.Text = "[温度]:" + weather2;//温度
			
			l_date.Text = wall[0];//日期
			l_w1.Text = wall[1];//风向
			l_w2.Text = wall[2];//风力
			l_w3.Text = wall[3]; //空气质量
			l_w4.Text = wall[4]; //紫外线强度
		}
	
		#endregion

		#region writeWeather 直接显示读取到的信息
		private void writeWeather(string city)
		{
			string wea_city = getWeatherByCity(city);
			Response.Write(wea_city);
		}
		#endregion
	
		#region Button1_Click
		protected void Button1_Click(object sender, System.EventArgs e)
		{
			writeWeatherInfo(ddlcity.SelectedValue);
			writeWeather(ddlcity.SelectedValue);
		}
		#endregion

		#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()
		{    

		}
		#endregion

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -