当前位置:K88软件开发文章中心编程语言.NETASP.NET → 文章内容

ASP.NET Web 服务

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-23 13:58:15

abel> <br /> <br /> <asp:Button ID="btnpostback" runat="server" Text="Post Back" style="width:132px" /> <asp:Button ID="btnservice" runat="server" Text="Get Stock" style="width:99px" /> </div> </form> </body> </html>web 应用的代码如下: using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; //this is the proxy using localhost; namespace wsclient { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { lblmessage.Text = "First Loading Time: " + DateTime.Now.ToLongTimeString } else { lblmessage.Text = "PostBack at: " + DateTime.Now.ToLongTimeString(); } } protected void btnservice_Click(object sender, EventArgs e) { StockService proxy = new StockService(); lblmessage.Text = String.Format("Current SATYAM Price:{0}", proxy.GetPrice("SATYAM").ToString()); } } }创建代理服务器代理服务器指的是一个 web 服务代码的代替者。在使用 web 服务之前,我们必须创建一个代理服务器。这个代理服务器是由客户端应用注册的。然后客户端应用实现调用 web 服务使之像在使用一个本地方法一样。 该代理服务器将调用,并用适当的格式将调用像发送 SOAP 请求一样发送到服务器。SOAP 支持简单对象访问协议(Simple Object Access Protocol)。该协议适用于 web 服务数据交换。当此服务器响应并返回一个 SOAP 包给客户端时,代理服务器将一切呈现给客户端应用程序。使用 btnservice_click 调用 Web 服务之前,Web 应用应该被添加到应用程序。这将透明地创建一个代理类,可由 btnservice_click 事件使用。 protected void btnservice_Click(object sender, EventArgs e) { StockService proxy = new StockService(); lblmessage.Text = String.Format("Current SATYAM Price: {0}", proxy.GetPrice("SATYAM").ToString()); }采取以下步骤创建代理: 步骤 (1) : 在解决方案管理器(SolutionExplorer)的 web 应用入口处右击选择 ‘Add Web Reference’。 步骤 (2) : 选择 ‘Web Services in this solution’,会返回我们编写的股票服务引用。 步骤 (3) : 点击该服务打开测试页面,创建代理时默认为 ‘localhost’,当然你也可以进行重命名。点击 ‘Add Reference’ 来实现向客户端应用程序添加一个代理。 在代码中加入以下语句使之包含该代理: using localhost;

上一页  [1] [2] 


ASP.NET Web 服务