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;
}
}
分享到:
相关推荐
用C#读取sina天气预告到wap页面 用C#读取sina天气预告到wap页面
c#读取二进制文件c#读取二进制文件c#读取二进制文件c#读取二进制文件c#读取二进制文件c#读取二进制文件
本程序主要是用webservices 实现的仿QQ的winform版的天气预报了
用C#写得天气预报,适用于初学者,同学们可以下载下看看
c#windowform天气预报,主要是从网络服务器获取天气情况,然后显示或写入数据库里。
利用C#实现天气预报 利用C#实现天气预报
天气预报工具 能时时知道天气 并有美好的界面, 和动态的移动界面 ,是C#学习人员的更好的参考学习资源 全部用C#完成
本工具使用C#语言开发WinFrom软件,根据国家气象局天气预报接口获取未来十五天天气预报。只用来新手学习,里边主要涉及到Url请求,Json转List,Json转model。
调用天气预报webservices通过引用web服务的方式获取到想要的城市天气情况 调用天气预报webservices通过引用web服务的方式获取到想要的城市天气情况
C#读取二代身份证信息源码
C#编写的桌面天气预报程序,经典实例,欢迎下载分享
参照这个网址http://zhangkui.blog.51cto.com/1796259/497324。实现C#调用WebService实现天气预报获取。完全能用。自己有编译通过。
基于C#开发的天气预报查看器窗体应用。通过调用第三方天气预报查看接口返回天气结果,能够查看给定城市当日天气和三日天气情况。内容包含C#代码和可执行程序。
C#357-天气预报程序源代码
这是用C#来读取WORD中的内容的可用程序
C#制作天气预报(根据城市不同动态得获得天气预报)
用C#写 的WebService天气预报功能
C# 读取加密的Excel 文件; 有源码,VS2010开发。 采用的是微软的Microsoft.Office.Interop.Excel, V14.0,来读取加密后的Excle文件。 不是采用第三方控件NPOI读取加密的Excel文件。 开始研究NPOI读取加密的Excel文件...
c# 天气预报查询 c# 天气预报查询 c# 天气预报查询 c# 天气预报查询
C#天气预报程序.rarC#天气预报程序.rarC#天气预报程序.rarC#天气预报程序.rarC#天气预报程序.rarC#天气预报程序.rar