用C#读取sina天气预报到wap页面

 
% @ Register TagPrefix = " mobile "  Namespace = " System.Web.UI.MobileControls "  Assembly = " System.Web.Mobile "   % >< % @ Page language = " c# "  Codebehind = " weather.aspx.cs "  Inherits = " goodwap.life.weather "  AutoEventWireup = " false "   % ><mobile:form id = " Form1 "  runat = " server "
<FONT face
= " 宋体 "
<mobile:Label id
= " Label3 "  runat = " server "  Font - Bold = " True " >【生活资讯】< / mobile:Label>
<mobile:Label id
= " Label1 "  runat = " server "  Font - Italic = " True " == 天气预报 == / mobile:Label>
<mobile:Label id
= " l_date "  runat = " server " >< / mobile:Label>
<mobile:Label id
= " l_city "  runat = " server " >< / mobile:Label>
<mobile:Label id
= " l_wea "  runat = " server " >< / mobile:Label>
<mobile:Label id
= " l_sky "  runat = " server " >< / mobile:Label>
<mobile:Label id
= " l_w1 "  runat = " server " >< / mobile:Label>
<mobile:Label id
= " l_w2 "  runat = " server " >< / mobile:Label>
<mobile:Label id
= " l_w3 "  runat = " server " >< / mobile:Label>
<mobile:Label id
= " Label2 "  runat = " server " >【请选择城市名】:< / mobile:Label>
<mobile:SelectionList id
= " s_weather "  runat = " server "
<Item Value
= " 深圳 "  Text = " 深圳 "  Selected = " True " >< / Item>
<Item Value
= " 广州 "  Text = " 广州 " >< / Item>
<Item Value
= " 上海 "  Text = " 上海 " >< / Item>
<Item Value
= " 北京 "  Text = " 北京 " >< / Item>
<Item Value
= " 天津 "  Text = " 天津 " >< / Item>
<Item Value
= " 武汉 "  Text = " 武汉 " >< / Item>
<Item Value
= " 重庆 "  Text = " 重庆 " >< / Item>
<Item Value
= " 成都 "  Text = " 成都 " >< / Item>
<Item Value
= " 南京 "  Text = " 南京 " >< / Item>
<Item Value
= " 香港 "  Text = " 香港 " >< / Item>
<Item Value
= " 澳门 "  Text = " 澳门 " >< / Item>
/ mobile:SelectionList>
<mobile:Command id
= " Command1 "  runat = " server " >查 询< / mobile:Command>
<mobile:Link id
= " Link1 "  runat = " server "  NavigateUrl = " ../default.aspx#Goodview " >首 页< / mobile:Link>< / FONT>< / mobile:form>

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(" ","");

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(" """);
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];
}

}


Web 窗体设计器生成的代码

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(" ","");

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(" """);
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;
= 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);
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;
}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以按照以下步骤使用 C# 读取 txt 文件中的经纬度,并将其在 WebBrowser 控件中显示标注: 1. 创建一个 Windows 窗体应用程序,并添加一个 WebBrowser 控件和一个按钮控件。 2. 在按钮的 Click 事件中添加以下代码,实现读取 txt 文件中的经纬度: ``` private void button1_Click(object sender, EventArgs e) { // 读取 txt 文件中的经纬度 string[] lines = File.ReadAllLines("data.txt"); foreach (string line in lines) { string[] parts = line.Split(','); double lat = double.Parse(parts[0]); double lng = double.Parse(parts[1]); // 在 WebBrowser 控件中显示标注 webBrowser1.Document.InvokeScript("addMarker", new object[] { lat, lng }); } } ``` 3. 在窗体的 Load 事件中添加以下代码,实现在 WebBrowser 控件中加载地图页面: ``` private void Form1_Load(object sender, EventArgs e) { // 加载地图页面 string html = "<html><head><script src=\"https://maps.googleapis.com/maps/api/js?key=<YOUR_API_KEY>\"></script><script>function initialize() { var mapOptions = { center: new google.maps.LatLng(0, 0), zoom: 2 }; var map = new google.maps.Map(document.getElementById(\"map-canvas\"), mapOptions); window.addMarker = function(lat, lng) { var marker = new google.maps.Marker({ position: new google.maps.LatLng(lat, lng), map: map }); }; } google.maps.event.addDomListener(window, 'load', initialize);</script></head><body><div id=\"map-canvas\" style=\"width:100%; height:100%;\"></div></body></html>"; webBrowser1.DocumentText = html; } ``` 4. 将 `<YOUR_API_KEY>` 替换为你的 Google Maps API 密钥。 5. 创建一个名为 `data.txt` 的文本文件,并将每个经纬度作为一行保存,格式为 `latitude,longitude`。 6. 运行程序,点击按钮即可在地图页面中显示标注。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值