webservice实现调用天气预报!!!急!!!高手赐教。。。

xuejie1989 2009-08-06 05:01:17
*********************************************************
用WebService实现天气预报的查询
这是获取天气Web服务代码,转自书上。
1。请在网站根目录下添加一个“Web服务”,命名为“Weather”,注意别建成aspx 页面。
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Net;
using System.IO;


/// <summary>
/// Weather 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Weather : System.Web.Services.WebService {

public Weather () {

//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}

[WebMethod]

public string GetWeather(string city)
{
string weacherhtml = string.Empty;
//转换输入参数的编码类型
string mycity = System.Web.HttpUtility.UrlEncode(city, System.Text.UnicodeEncoding.GetEncoding("GB2312"));
//初始化新的 WebRequest
HttpWebRequest webrt = (HttpWebRequest)WebRequest.Create("http://php.weather.sina.com.cn/search.php?city=" + mycity);
HttpWebResponse webrs = (HttpWebResponse)webrt.GetResponse();
//从Internet资源返回数据流
Stream stream = webrs.GetResponseStream();
//读取数据流
StreamReader srm = new StreamReader(stream, System.Text.Encoding.Default);
//读取数据
weacherhtml = srm.ReadToEnd();
srm.Close();
stream.Close();
webrs.Close();
//针对不同的网站,请查看HTML源文件
int start = weacherhtml.IndexOf("天气状况 begin"); //取的字符位置
int end = weacherhtml.IndexOf("天气状况 end");
return weacherhtml.Substring(start + 14, end - start);
}
}
2。添加Web引用,选择“此解决方案中的 Web 服务 ”这个时候,会自动找出“Weather”这个服务。
3。新建ASPX页面,在页面上放Button1、Label1、TextBox1这三个控件。代码如下:

protected void Button1_Click(object sender, EventArgs e)
{
Weather myWeather = new Weather();
Label1.Text = myWeather.GetWeather(TextBox1.Text);
}

这个时候运行试下,就可以看到,调用新浪的天气预报了。
*************************************************************
以上是我在网上搜的一篇文章,由于初学,一些地方不太懂,请高手解释清楚点。。。
我是这样做的:
打开vs2005-》文件-》新建-》网站-》asp.net.web服务-》然后添加web服务->书写上面的代码(请问这段代码到底写到哪里??)-》(如何新建ASPX页面????)
请高手教教我,别笑话我哦~~~嘻嘻。。。


...全文
647 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuejie1989 2009-08-06
  • 打赏
  • 举报
回复
晕。。。。。。。。。。。。。。。。。
fswangbaochang 2009-08-06
  • 打赏
  • 举报
回复
你用读取的是新浪天预报吧,新浪的天气预备可能做了限制了,不让从外部链接读取他们网页的内容了,人是去年做过一个类似的程序,去年是可以读取的,今年不知什么时候不行了,建议你改用别的天气预报网站行抓取
xuejie1989 2009-08-06
  • 打赏
  • 举报
回复
能从winform里调用吗?能给我截图讲讲么。。。。万分感谢!
jerrylyj 2009-08-06
  • 打赏
  • 举报
回复
建立网站总会吧?
1、见一个站点,右键选择站点,选择添加项,选择添加Web服务,命名该服务为Weather,ok
2、出现一个页面,把上面代码复制进去。
3、右键单击站点,选择添加Web服务引用,选择解决方案中的服务,选择刚才建的weather
4、ok,已经引用好了,可以拿来用了。
5、右键站点,新建一个Web页面,aspx的,拉一个label过去,在pageload中添加代码
Weather myWeather = new Weather();
Label1.Text = myWeather.GetWeather(TextBox1.Text);
ok????

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧