日期:2014-05-18  浏览次数:20886 次

WebService天气预报
protected void Page_Load(object sender, EventArgs e)
  {
  //实例化地址服务
  IpAddressSearchWebService ipaddress = new IpAddressSearchWebService();
  //获取本地IP详细地址
  string city = GetCity(ipaddress.getGeoIPContext()[1]);
  ShowWeather(city);
  }
  //显示天气预报
  public void ShowWeather(string city)
  {

  ////获得指定地址的天气预报
  string[] weathers = GetWatherByCity(city);
  //获取有效时间
  indate.Text = weathers[5].ToString().Replace("/", "~");
  country.Text = weathers[1].ToString();
  Image1.ImageUrl = @"weatherImage/" + weathers[8].ToString();



  }

  //按城市返回天气信息
  public string[] GetWatherByCity(string city)
  {
  WeatherWebService weather = new WeatherWebService();
  return weather.getWeatherbyCityName(city);


  }

  //地址字符串处理
  public string GetCity(string city)
  {

  int index = city.IndexOf("省") == -1 ? city.IndexOf("县") : city.IndexOf("省");

  int end = city.IndexOf("市") == -1 ? city.IndexOf("县") : city.IndexOf("市"); ;

  if (index != -1 && end != -1)
  {

  return city.Remove(end).Substring(index + 1);
  }
  if (end == -1 && index != -1)
  {
  return city.Substring(index);
  }

  if (end != -1 && index == -1)
  {
  return city.Remove(end);
  }


  else
  {

  string[] s = city.Split(new char[] { ' ' });
  return s[0];
  }

  }

为什么总是返回北京的天气

------解决方案--------------------
嘻嘻,还有这样的方法的呀。。。看来可以加到我的那个小站去了。学习了
------解决方案--------------------
看看传递的数据
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx 
static void Main(string[] args)
{
WeatherWebService wws = new WeatherWebService();
string[] weatherArray = wws.getWeatherbyCityName("");
Console.WriteLine("{0}-{1}-{2}", weatherArray[6], weatherArray[7], weatherArray[8]);
Console.Read();
}

------解决方案--------------------
WeaterhService.WeatherWebServiceSoapClient weather = new Weather.WeaterhService.WeatherWebServiceSoapClient("WeatherWebServiceSoap");
string cityName = this.txtCityName.Text.Trim();
string[] str = new string[23];
str = weather.getWeatherbyCityName(cityName);

this.txtProvince.Text = str[0];
this.txtCity.Text = str[1];
this.txtCityID.Text = str[2];
this.txtDate.Text = str[4];
this.txtTodayWeather.Text = str[5];
this.txtTodayWind.Text = str[7];

this.txttomWenDu.Text = str[12];
this.txtTomWeather.Text = str[13];
this.txtTomWind.Text = str[14];

this.txtHouTianWendu.Text = str[17];
this.txtHoutianWeather.Text = str[18];
this.txtHoutianWind.Text = str[19];

this.richTextBox1.Text = str[11];
------解决方案--------------------
可能是IP地址问题,你的IP地址是哪里的,并不是取决于地理位置,很多情况下,北方某些地方的IP地址,反映过来都是其他地方的,尤其如果你的IP地址如果是通过DHCP自动获取的,那就更没准了