C#写一个URL编码转换GB23121的方法,然后可以取到天气预报

  /// <summary>
  /// 根据城市取得天气预报信息
  /// </summary>
  /// <param name="ps_CityName">城市中文名称 如:福州</param>
  /// <returns></returns>
  public static string GetSinaWether(string ps_CityName)
  {
   string strCity = StrConv(ps_CityName,"GB2312");
   
   //string myuri="http://appnews.qq.com/cgi-bin/news_qq_search?city="+city;
   string myuri="http://weather.news.sina.com.cn/cgi-bin/figureWeather/simpleSearch.cgi?city="+strCity;   
   WebRequest webr=WebRequest.Create(myuri);
   

   Stream rc=webr.GetResponse().GetResponseStream();
   
   StreamReader read=new StreamReader(rc,System.Text.Encoding.Default);
   string abc=read.ReadToEnd();
   int l1=abc.IndexOf("今日天气");
   string restr="<font face=黑体 style=font-size:18px;>"+abc.Substring(l1).Replace("<br>","").Replace("<td style=","").Replace("class=l15>","").Replace("color:#083887","").Replace("</td>","").Replace("</tr>","").Replace("</table>","").Replace("</body>","").Replace("</html>","");
   return restr.Replace("50","20");
  }

  /// <summary>
  /// 根据城市取得天气预报信息
  /// </summary>
  /// <param name="ps_CityName">城市中文名称 如:福州</param>
  /// <returns></returns>
  public static string GetQQWether(string ps_CityName)
  {
   string strCity = StrConv(ps_CityName,"GB2312");
   
   string myuri="http://appnews.qq.com/cgi-bin/news_qq_search?city="+strCity;
   WebRequest webr=WebRequest.Create(myuri);
   
   Stream rc=webr.GetResponse().GetResponseStream();
   
   StreamReader read=new StreamReader(rc,System.Text.Encoding.Default);
   string abc=read.ReadToEnd();
   return abc;
  }

  /// <summary>
  /// 编码转换
  /// </summary>
  /// <param name="strIn"></param>
  /// <param name="encoding"></param>
  /// <returns></returns>
  public static string StrConv(string strIn, string encoding)
  {
   return System.Web.HttpUtility.UrlEncode(strIn,System.Text.Encoding.GetEncoding(encoding));
  }

posted @ 2005-01-19 11:08  小草  阅读(5122)  评论(8编辑  收藏  举报
Google+