public class weather : System.Web.UI.MobileControls.MobilePage
{
protected System.Web.UI.MobileControls.Label Label1;
protected System.Web.UI.MobileControls.Label Label2;
protected System.Web.UI.MobileControls.SelectionList s_weather;
protected System.Web.UI.MobileControls.Label l_date;
protected System.Web.UI.MobileControls.Label l_city;
protected System.Web.UI.MobileControls.Label l_wea;
protected System.Web.UI.MobileControls.Label l_sky;
protected System.Web.UI.MobileControls.Label l_w1;
protected System.Web.UI.MobileControls.Label l_w2;
protected System.Web.UI.MobileControls.Label l_w3;
protected System.Web.UI.MobileControls.Link Link1;
protected System.Web.UI.MobileControls.Command Command1;
protected System.Web.UI.MobileControls.Label Label3;
protected System.Web.UI.MobileControls.Form Form1; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!Page.IsPostBack)
{
string city = "深圳";
int start,stop;
string weather1,weather2,wea;
string wea_city = weather_city(city);
wea_city = wea_city.Replace(&quot; &quot;,&quot;&quot;); start = wea_city.IndexOf(&quot;<b>&quot;,0,wea_city.Length);
stop = wea_city.IndexOf(&quot;</b>&quot;, start);
weather1 = wea_city.Substring(start, stop-start).Trim() &quot; &quot;;
weather1 = weather1.Substring(3,8).Trim();
start = wea_city.IndexOf(&quot;<tdstyle=\&quot;font-size:40px;font-family:TimesNewRoman;font-weight:bold;\&quot;>&quot;,0,wea_city.Length);
stop = wea_city.IndexOf(&quot;℃&quot;,start) 40;
weather2 = wea_city.Substring(start, stop-start);
weather2 = weather2.Substring(stop-start-42,40).Trim();
weather2 = weather2.Replace(&quot;\t&quot;,&quot;&quot;); start = wea_city.IndexOf(&quot;<fontcolor=#183888><b>&quot;, 0, wea_city.Length);
stop = wea_city.IndexOf(&quot;</b></font>&quot;,start);
wea = wea_city.Substring(start,stop-start);
wea = wea.Substring(22,wea.Length-22) &quot;kbrk&quot;;
wea = wea.Replace(&quot;\t&quot;, &quot;&quot;);
wea = wea.Replace(&quot;>&quot;, &quot;k&quot;);
wea = wea.Replace(&quot;<&quot;, &quot;k&quot;);
wea = wea.Replace(&quot;kbrk&quot;, &quot;k&quot;);
string [] wall = null;
char[] seperator = {'k'};
wall = wea.Split(seperator); //////////////////////////////////////
l_city.Text = &quot;[城市]:&quot; city;
l_wea.Text = &quot;[天气]:&quot; weather1;
l_sky.Text = &quot;[温度]:&quot; weather2;
///////
l_date.Text = wall[0];
l_w1.Text = wall[1];
l_w2.Text = wall[2];
l_w3.Text = wall[3];
}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
} /// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Command1.Click = new System.EventHandler(this.Command1_Click);
this.Load = new System.EventHandler(this.Page_Load); }
#endregion
private void Command1_Click(object sender, System.EventArgs e)
{
string city = s_weather.Selection.Value.Trim();
int start,stop;
string weather1,weather2,wea;
string wea_city = weather_city(city);
wea_city = wea_city.Replace(&quot; &quot;,&quot;&quot;); start = wea_city.IndexOf(&quot;<b>&quot;,0,wea_city.Length);
stop = wea_city.IndexOf(&quot;</b>&quot;, start);
weather1 = wea_city.Substring(start, stop-start).Trim() &quot; &quot;;
weather1 = weather1.Substring(3,8).Trim();
start = wea_city.IndexOf(&quot;<tdstyle=\&quot;font-size:40px;font-family:TimesNewRoman;font-weight:bold;\&quot;>&quot;,0,wea_city.Length);
stop = wea_city.IndexOf(&quot;℃&quot;,start) 40;
weather2 = wea_city.Substring(start, stop-start);
weather2 = weather2.Substring(stop-start-42,40).Trim();
weather2 = weather2.Replace(&quot;\t&quot;,&quot;&quot;); start = wea_city.IndexOf(&quot;<fontcolor=#183888><b>&quot;, 0, wea_city.Length);
stop = wea_city.IndexOf(&quot;</b></font>&quot;,start);
wea = wea_city.Substring(start,stop-start);
wea = wea.Substring(22,wea.Length-22) &quot;kbrk&quot;;
wea = wea.Replace(&quot;\t&quot;, &quot;&quot;);
wea = wea.Replace(&quot;>&quot;, &quot;k&quot;);
wea = wea.Replace(&quot;<&quot;, &quot;k&quot;);
wea = wea.Replace(&quot;kbrk&quot;, &quot;k&quot;);
string [] wall = null;
char[] seperator = {'k'};
wall = wea.Split(seperator); //////////////////////////////////////
l_city.Text = &quot;[城市]:&quot; city;
l_wea.Text = &quot;[天气]:&quot; weather1;
l_sky.Text = &quot;[温度]:&quot; weather2;
///////
l_date.Text = wall[0];
l_w1.Text = wall[1];
l_w2.Text = wall[2];
l_w3.Text = wall[3];
} public string weather_city(string city)
{
string temp = null;
try
{
string strURL = &quot;http://weather.news.sina.com.cn/cgi-bin/figureWeather/search.cgi&quot;;;
HttpWebRequest request;
request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method=&quot;POST&quot;; //Post请求方式
request.ContentType=&quot;application/x-www-form-urlencoded&quot;; //内容类型
string paraUrlCoded = System.Web.HttpUtility.UrlEncode(&quot;city&quot;); //参数经过URL编码
paraUrlCoded = paraUrlCoded &quot;=&quot; System.Web.HttpUtility.UrlEncode(city, System.Text.Encoding.GetEncoding(&quot;GB2312&quot;));
byte[] payload;
payload = System.Text.Encoding.GetEncoding(&quot;GB2312&quot;).GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节
request.ContentLength = payload.Length; //设置请求的ContentLength
Stream writer = request.GetRequestStream(); //获得请求流
writer.Write(payload,0,payload.Length); //将请求参数写入流
writer.Close(); //关闭请求流
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse(); //获得响应流
Stream s;
s = response.GetResponseStream();
StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding(&quot;GB2312&quot;));
string HTML = &quot;&quot;;
string sLine = &quot;&quot;;
int i = 0;
while (sLine!=null)
{
i ;
sLine = objReader.ReadLine();
if (sLine!=null)
HTML = sLine;
}
HTML = HTML.Replace(&quot;&amp;;amp;lt;&quot;,&quot;<&quot;);
HTML = HTML.Replace(&quot;&amp;;amp;gt;&quot;,&quot;>&quot;);
int start,stop;
start = HTML.IndexOf(&quot;<img src=\&quot;http://image2.sina.com.cn/dy/weather/images/figure/&quot;;,0,HTML.Length);
stop = HTML.IndexOf(&quot;<td background=http://image2.sina.com.cn/dy/weather/images&quot;,start);
temp = HTML.Substring(start, stop - start);
}
catch (Exception x)
{
}
return temp;
}
}