vs2017调用天气预报服务
1、先添加服务引用
2、点击高级
3、添加web引用
4、输入服务地址,添加引用
后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.WebSockets;
using System.Web.Services;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
getWeather();
}
public string getWeather()
{
StringBuilder sb = new StringBuilder();
string[] WI = new string[22];//声明string数组储存放返回结果
cn.com.webxml.www.WeatherWebService s = new cn.com.webxml.www.WeatherWebService();
string CityName = "北京";
WI = s.getWeatherbyCityName(CityName);
sb.Append(string.Format(@"<table>
<tr><td>今天天气概况:</td><td><span>{0}</span> <span><img src=""image/weather/{1}""/> <img src=""image/weather/{2}""/></span></td></tr>
<tr><td>今天天气实况:</td><td>{3}</td></tr>
<tr><td>明天天气概况:</td><td><span>{4}</span> <span><img src=""image/weather/{5}""/> <img src=""image/weather/{6}""/></span></td></tr>
<tr><td>后天天气概况:</td><td><span>{7}</span> <span><img src=""image/weather/{8}""/> <img src=""image/weather/{9}""/></span></td></tr>
</table>", WI[6], WI[8], WI[9], WI[10], WI[13], WI[15], WI[16], WI[18], WI[20], WI[21]));
return sb.ToString();
}
}
}
前台页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=getWeather()%>
</div>
</form>
</body>
</html>
效果