110,568
社区成员
发帖
与我相关
我的任务
分享
WindowsApplication2.com.wopos.www.Weather aa =
new WindowsApplication2.com.wopos.www.Weather();
this.textBox2.Text = aa.getWeather("乌鲁木齐");
aa.Dispose();
[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);
}