HttpWebRequest请求连接天气预报接口
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
1、webservice服务地址,直接添加服务引用
public static WeatherModel GetWeatherData(string cityName) { WeatherModel model = new WeatherModel(); try { ServiceReference1.WeatherWebServiceSoapClient client = new ServiceReference1.WeatherWebServiceSoapClient(); string[] data = client.getWeatherbyCityName(cityName);//此方法不起作用,报错 } catch (System.ServiceModel.FaultException ex) { throw ex; } return model; }
2、调用web地址,HttpWebRequest和HttpRequest区别:
HttpRequest:封装游览器对服务器的请求,其中包括游览器请求的网址,查询字符串或者表单
HttpWebRequest:则是用来简化网络请求的过程,从服务器上获取文件/结果的,譬如你可以在代码中用这个类冒充浏览器(设置一个UserAgent)来发请求
<appSettings>
<add key="webWeatherByCityName" value="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName"/>
</appSettings>
public static WeatherModel RequestWeatherData(string cityName) { WeatherModel model = new WeatherModel(); string result = ""; string url = ConfigurationManager.AppSettings["webWeatherByCityName"]; HttpWebRequest request = HttpWebRequest.Create(url+"?theCityName=" + cityName) as HttpWebRequest; request.UserAgent = "MSIE6.0";//冒充IE6访问,不写就报错 request.Method = "get"; request.ContentType = "text/xml; charset=utf-8"; HttpWebResponse response; try { using (response = request.GetResponse() as HttpWebResponse) { using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } } } } catch (WebException ex) { response= ex.Response as HttpWebResponse; } string[] data = SerializeDataXmlStr(result); if (data.Length == 23) { model.ID = Guid.NewGuid(); model.Province = data[0]; model.City = data[1]; model.CityCode = data[2]; model.CityCard = data[3]; model.LastUpdateTime = DateTime.Parse(data[4]); WeatherCondition day1 = new WeatherCondition(); day1.Day = DateTime.Now; day1.WeatherID = model.ID; day1.Temperature = data[5]; day1.Condition = data[6]; day1.Wind = data[7]; day1.BeginTrend = data[8]; day1.EndTrend = data[9]; day1.Live = data[10]; day1.LivingIndex = data[11]; model.ConditionList = new List<WeatherCondition>(); model.ConditionList.Add(day1); WeatherCondition day2 = new WeatherCondition(); day2.Day = DateTime.Now.AddDays(1); day2.WeatherID = model.ID; day2.Temperature = data[12]; day2.Condition = data[13]; day2.Wind = data[14]; day2.BeginTrend = data[15]; day2.EndTrend = data[16]; model.ConditionList.Add(day2); WeatherCondition day3 = new WeatherCondition(); day3.Day = DateTime.Now.AddDays(2); day3.WeatherID = model.ID; day3.Temperature = data[17]; day3.Condition = data[18]; day3.Wind = data[19]; day3.BeginTrend = data[20]; day3.EndTrend = data[21]; model.ConditionList.Add(day3); model.Introduce = data[22]; } return model; }
xml字符串转换为一个数组
public static string[] SerializeDataXmlStr(string xml) { XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); var nodes = doc.DocumentElement.ChildNodes; string[] items = new string[doc.DocumentElement.ChildNodes.Count]; int i = 0; foreach (XmlElement node in nodes) { items[i++] = node.InnerText; } return items; }

浙公网安备 33010602011771号