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(" ","");
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];
}
}
#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(" ","");
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];
}
public string weather_city(string city)
{
string temp = null;
try
{
string strurl = "http://weather.news.sina.com.cn/cgi-bin/figureweather/search.cgi";
httpwebrequest request;
request = (httpwebrequest)webrequest.create(strurl);
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;
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;
response = (httpwebresponse)request.getresponse(); //获得响应流
stream s;
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("<","<");
html = html.replace(">",">");
int start,stop;
start = html.indexof("<img src=/"http://image2.sina.com.cn/dy/weather/images/figure/",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;
}
}
新闻热点
疑难解答