C# VS2015之Webservices调用WeatherWs案例(实时显示)
**序言:**由于学习体系结构,老师在讲解实验中Web服务的应用实例中,通过调用天气预报接口让我们学习如何在大佬的肩膀上,拔得头筹(涨工资,有个破车开)。
-
第一步,需要电脑上有一个VS2015。(重点是你要开发C#,
所以刚开始一定要选择C#开发那个选项)
http://download.microsoft.com/download/B/4/8/B4870509-05CB-447C-878F-2F80E4CB464C/vs2015.com_chs.iso -
下载完事后才是重点,新建一个项目(Visual C#–>ASP.NET Web 应用),一定要记得项目名,记得和你需要的项目名一致,我们建立项目名为soapTry2
如果出现如下问题,别慌,没有账户,不要就是了,我们继续。不惯着它,***取消***就好了 ,此刻我们的项目就算建立完成了
-
在这个项目下新建一个Web服务(ASMX),命名为WebServicesTest.(右键你的项目–>添加–>新建项)不出意外的话是下面的界面:
-
看一下我们刚才的成果:
-
下面我们来添加一个***服务引用***(项目–>添加–>服务引用)然后将天气预报接口的地址http://ws.webxml.com.cn/WebServices/WeatherWS.asmx复制进去 点击转到。将会有下面这个页面:
-
这个时候不要心急,点击(***高级(V)…***)
会出现如下界面
-
接着点击 ***添加Web引用(W)…***,见下图:
此图是将天气预报的URL地址复制到URL出点击转到,之后的结果。
-
下面建立一个Web窗体(项目–>添加–>Web 窗体),命名为Test.aspx 它的作用就是为天气显示提供一个场地。
建立完成后如下图:
-
好了哦,工程建好之后,我们需要修改一下代码:
找到webServiceTest.asmx这个文件,我们找一下,如下图
将上面的代码换成如下所示的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace soapTry
{
/// <summary>
/// WebServiceTest的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class WebServiceTest: System.Web.Services.WebService
{
[WebMethod]
public string ReverseString(string message) //新建这个方法,字符串反转的功能
{
char[] arr = message.ToCharArray();
Array.Reverse(arr);
message = new string(arr);
return message;
}
[WebMethod]
public string login(string i_tel,string i_psd) //登录服务,提供测试登录的功能
{
string tel = i_tel.Trim().ToString();
string psd = i_psd.Trim().ToString();
if (tel.Equals("admin") & psd.Equals("asd"))
{
return "Success";
}
Else
{
return "False";
}
}
}
}
对于这段代码,我们需要了解的就是反转字符串的代码,每一个[WebMethod]就对应了一种功能。
- 下面是对界面的显示:
找到一个Test.aspx那个文件打开看一下,顺便加上一个Label标签,以及对于实时显示时间的div模块。啥也不说了,上代码吧!
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="soapTry.Test" %>
<!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> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> </div>
<table >
<tr>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Timer ID="Timer1" runat="server" Interval="1000"> </asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
</tr>
</table>
<div>
<asp:Label ID="show_message" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
- 在Test.aspx隔壁还有一个Test.aspx.cs文件,我们再把这里面的代码改一下,上修改后的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//下面的代码是使用了实时显示时间的命名空间
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace soapTry
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
{
Label1.Text = DateTime.Now.ToString();
}
//WebServiceTest test = new WebServiceTest();
// show_message.Text = test.login("admin", "asd");
soapTry2.cn.com.webxml.ws.WeatherWS ws = new soapTry2.cn.com.webxml.ws.WeatherWS();
String[] str = ws.getWeather("1436","");//这个也是一个重点,两个参数,一个填你想调用城市的代码,另一个为空就好了
show_message.Text = "专业:软件工程 学号:1314 姓名:你算哪课小白菜 ";
for (int i = 0; i < str.Length; i++)
{
show_message.Text += str[i]+"<br/>" ; //遍历结果数组,依次输出 API 服务里获取的地区结果
}
}
}
}
- 兄弟,我告诉你,如果你按着老铁我的步骤来,这会儿,你该偷笑了吧!!! 十月一假期的作业就这样完事了!!!
不信????运行一下试试!!
为老铁们在附上一张运行结果图啵!!!
最后感谢王老师的案例,以及表达对您的佩服。