一:用ASP.NET调用Web Service

打开VS2010,打开“文件-新建-网站”,选择“ASP.NET网站”

选好存储位置,语言后点击确定,进入默认页面。然后先添加Web引用,把WebService引到当前的工程里面。方法是:在资源管理器中点击右键,选择添加Web 引用,(该webservice为上一篇的例子)调出对话框:

在URL中填入,前面写好的WebService运行后浏览器上面显示的地址,点击“前往”按钮,如上图,就会显示出所引用的WebService中可以调用的方法,然后点击“添加引用”,就将webservice引用到了当前的工程里面 ,如下图,解决方案中会出现引进来的WebService文件

如果添加的是ServiceReference的服务引用,实例化服务是应该这样,如下:

  1.         hong.Service1SoapClient login = new hong.Service1SoapClient();
  2. if (txtName.Text.Trim().Length == )
  3. {
  4. MessageBox.Show("请输入用户名!");
  5. return;
  6. }
  7. else if (pwdInfo.Password.Trim().Length == )
  8. {
  9. MessageBox.Show("请输入密码!");
  10. return;
  11. }

我们在这就练习调用webservice的四个方法,做一个简单的调用的例子,先在网站的前台添加几个控件,代码如下:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" >
  5. <head id="Head1" runat="server">
  6. <title>Webservice调用实例</title>
  7. </head>
  8. <body>
  9. <form id="form2" runat="server">
  10. <div>
  11. <asp:TextBox ID="Num1" runat="server"></asp:TextBox>
  12. <select id="selectOper" runat = "server">
  13. <option>+</option>
  14. <option>-</option>
  15. <option>*</option>
  16. <option>/</option>
  17. </select>
  18. <asp:TextBox ID="Num2" runat="server"></asp:TextBox>
  19. <span id = E runat = "server"></span>
  20. <asp:TextBox ID="Result" runat="server"></asp:TextBox>
  21. </div>
  22. </form>
  23. </body>
  24. </html>

然后在后台写调用的代码,调用之前和使用其它的对象一样,要先实例化,实例化的方法是localhost.Service a =new localhost.Service();然后就可以通过a来访问WebService里面提供的方法了。在这个例子里面,动态的创建了一个button控件来触发WebService的调用,后台代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6.  
  7. namespace WebApplication2
  8. {
  9. public partial class _Default : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13.  
  14. //在页面加载的时候动态创建一个按钮,在它的事件里调用Webservice
  15. Button btn = new Button();
  16. btn.Width = ;
  17. btn.Text = " = ";
  18. btn.Click += new EventHandler(btn_Click);
  19. E.Controls.Add(btn);
  20.  
  21. }
  22.  
  23. /// <summary>
  24. /// 定义动态创建Button的Click事件,在这个事件中调用Webservice
  25. /// </summary>
  26. /// <param name="sender"></param>
  27. /// <param name="e"></param>
  28. void btn_Click(object sender, EventArgs e)
  29. {
  30. if (Num1.Text != "" && Num2.Text != "")
  31. {
  32. //实例化引用的webservice对象
  33. localhost.Service1 WebserviceInstance = new localhost.Service1();
  34. int Oper = selectOper.SelectedIndex;
  35. switch (Oper)
  36. {
  37. //通过实例化的webservice对象来调用Webservice暴露的方法
  38. case :
  39. Result.Text = WebserviceInstance.addition(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
  40. break;
  41. case :
  42. Result.Text = WebserviceInstance.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
  43. break;
  44. case :
  45. Result.Text = WebserviceInstance.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
  46. break;
  47. case :
  48. Result.Text = WebserviceInstance.division(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
  49. break;
  50. }
  51. }
  52. }
  53. }
  54. }

运行后可以看到效果,如下图所示,在前面两个Textbox里面输入两个操作数,在中间的下拉列表中选择操作符,然后点击“=”号,将计算的结果输出到第三个Textbox里面。

其中的报错解决

整个计算并不是在本地进行的,是在Web服务端进行计算的然后将结果通过XML返还给了调用方的,所以,在运行该程序的时候,WebService程序还必须启动,否则会报无法连接远程服务器的异常

二:winform程序调用Web Service

运行效果图:

新建一个winform项目,添加一个添加Web引用,和ASP.Net添加引用相同,使用的同样是上一篇的webservic

e

界面设计代码:

  1. namespace winform
  2. {
  3. partial class Form1
  4. {
  5. /// <summary>
  6. /// 必需的设计器变量。
  7. /// </summary>
  8. private System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// 清理所有正在使用的资源。
  12. /// </summary>
  13. /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing && (components != null))
  17. {
  18. components.Dispose();
  19. }
  20. base.Dispose(disposing);
  21. }
  22.  
  23. #region Windows 窗体设计器生成的代码
  24.  
  25. /// <summary>
  26. /// 设计器支持所需的方法 - 不要
  27. /// 使用代码编辑器修改此方法的内容。
  28. /// </summary>
  29. private void InitializeComponent()
  30. {
  31. this.btnCompute = new System.Windows.Forms.Button();
  32. this.tbNumX = new System.Windows.Forms.TextBox();
  33. this.tbNumY = new System.Windows.Forms.TextBox();
  34. this.tbResult = new System.Windows.Forms.TextBox();
  35. this.label2 = new System.Windows.Forms.Label();
  36. this.cmbWays = new System.Windows.Forms.ComboBox();
  37. this.SuspendLayout();
  38. //
  39. // btnCompute
  40. //
  41. this.btnCompute.Location = new System.Drawing.Point(, );
  42. this.btnCompute.Name = "btnCompute";
  43. this.btnCompute.Size = new System.Drawing.Size(, );
  44. this.btnCompute.TabIndex = ;
  45. this.btnCompute.Text = "计算";
  46. this.btnCompute.UseVisualStyleBackColor = true;
  47. this.btnCompute.Click += new System.EventHandler(this.btnCompute_Click);
  48. //
  49. // tbNumX
  50. //
  51. this.tbNumX.Location = new System.Drawing.Point(, );
  52. this.tbNumX.Name = "tbNumX";
  53. this.tbNumX.Size = new System.Drawing.Size(, );
  54. this.tbNumX.TabIndex = ;
  55. //
  56. // tbNumY
  57. //
  58. this.tbNumY.Location = new System.Drawing.Point(, );
  59. this.tbNumY.Name = "tbNumY";
  60. this.tbNumY.Size = new System.Drawing.Size(, );
  61. this.tbNumY.TabIndex = ;
  62. //
  63. // tbResult
  64. //
  65. this.tbResult.Location = new System.Drawing.Point(, );
  66. this.tbResult.Name = "tbResult";
  67. this.tbResult.Size = new System.Drawing.Size(, );
  68. this.tbResult.TabIndex = ;
  69. //
  70. // label2
  71. //
  72. this.label2.AutoSize = true;
  73. this.label2.Location = new System.Drawing.Point(, );
  74. this.label2.Name = "label2";
  75. this.label2.Size = new System.Drawing.Size(, );
  76. this.label2.TabIndex = ;
  77. this.label2.Text = "=";
  78. //
  79. // cmbWays
  80. //
  81. this.cmbWays.FormattingEnabled = true;
  82. this.cmbWays.Location = new System.Drawing.Point(, );
  83. this.cmbWays.Name = "cmbWays";
  84. this.cmbWays.Size = new System.Drawing.Size(, );
  85. this.cmbWays.TabIndex = ;
  86. //
  87. // Form1
  88. //
  89. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
  90. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  91. this.ClientSize = new System.Drawing.Size(, );
  92. this.Controls.Add(this.cmbWays);
  93. this.Controls.Add(this.label2);
  94. this.Controls.Add(this.tbResult);
  95. this.Controls.Add(this.tbNumY);
  96. this.Controls.Add(this.tbNumX);
  97. this.Controls.Add(this.btnCompute);
  98. this.Name = "Form1";
  99. this.Text = "Form1";
  100. this.Load += new System.EventHandler(this.Form1_Load);
  101. this.ResumeLayout(false);
  102. this.PerformLayout();
  103.  
  104. }
  105.  
  106. #endregion
  107.  
  108. private System.Windows.Forms.Button btnCompute;
  109. private System.Windows.Forms.TextBox tbNumX;
  110. private System.Windows.Forms.TextBox tbNumY;
  111. private System.Windows.Forms.TextBox tbResult;
  112. private System.Windows.Forms.Label label2;
  113. private System.Windows.Forms.ComboBox cmbWays;
  114. }
  115. }

后台代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace winform
  10. {
  11. public partial class Form1 : Form
  12. {
  13. //Sign s = new Sign();
  14. public Form1()
  15. {
  16. InitializeComponent();
  17.  
  18. }
  19.  
  20. private void btnCompute_Click(object sender, EventArgs e)
  21. {
  22. if (tbNumX.Text != "" && tbNumY.Text != "")
  23. {
  24. //实例化引用的webservice对象
  25. Compute.Service1 WebserviceInstance = new Compute.Service1 ();
  26. int a =Convert.ToInt32( cmbWays.SelectedItem.ToString().Trim().Substring(,));
  27.  
  28. switch (a)
  29. {
  30. //通过实例化的webservice对象来调用Webservice暴露的方法
  31. case :
  32. tbResult.Text = WebserviceInstance.addition(double.Parse(tbNumX.Text), double.Parse(tbNumY.Text)).ToString();
  33. break;
  34. case :
  35. tbResult.Text = WebserviceInstance.subtract(double.Parse(tbNumX.Text), double.Parse(tbNumY.Text)).ToString();
  36. break;
  37. case :
  38. tbResult.Text = WebserviceInstance.multiplication(double.Parse(tbNumX.Text), double.Parse(tbNumY.Text)).ToString();
  39. break;
  40. case :
  41. tbResult.Text = WebserviceInstance.division(double.Parse(tbNumX.Text), double.Parse(tbNumY.Text)).ToString();
  42. break;
  43. }
  44. }
  45. }
  46. //载入加减乘除的符号
  47. private void Form1_Load(object sender, EventArgs e)
  48. {
  49. Sign add = new Sign { Name = "+", Num = };
  50. Sign sub = new Sign { Name = "-", Num = };
  51. Sign mul = new Sign { Name = "*", Num = };
  52. Sign div = new Sign { Name = "/", Num = };
  53.  
  54. //cmbWays.Items.Add(add);
  55.  
  56. cmbWays.Items.Add(add.Num+"---> "+ add.Name);
  57. cmbWays.Items.Add(sub.Num + "---> " + sub.Name);
  58. cmbWays.Items.Add(mul.Num + "---> " + mul.Name);
  59. cmbWays.Items.Add(div.Num + "---> " + div.Name);
  60. cmbWays.SelectedItem = add.Num + "---> " + add.Name;//去掉了空格,默认选择项
  61.  
  62. }
  63. }
  64. }

自定义类:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace winform
  6. {
  7. public class Sign
  8. {
  9. public int Num { get; set; }
  10. public string Name { get; set; }
  11. }
  12. }

相关的文章还有:

winform学习日志(二十五)----------C#调用webservers实现天气预报

C#之VS2010ASP.NET页面调用Web Service和winform程序调用Web Service的更多相关文章

  1. c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程

    c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...

  2. 一个简单的winform程序调用webservices

    本文原创,如需转载,请标明源地址,谢谢合作!http://blog.csdn.net/sue_1989/article/details/6597078 本文的编写IDE为VSTS2008和.NET F ...

  3. HTML5——web存储 Web SQL 数据库 应用程序缓存 Web Workers 服务器发送事件 WebSocket

    web存储 比cookie更好的本地存储方式 localStorage - 用于长久保存整个网站的数据,保存的数据没有过期时间,直到手动去除. sessionStorage - 用于临时保存同一窗口( ...

  4. c#WinForm程序调用vsto动态库,已解决

    最近做一个vsto的项目,涉及到Form程序调用vsto动态库,弄了半天,搜了很多资料终于搞定了,把积累写下来备以后用.相关网址: https://stackoverflow.com/question ...

  5. Web应用程序或者WinForm程序 调用 控制台应用程序及参数传递

    有时候在项目中,会调用一个控制台应用程序去处理一些工作.那在我们的程序中要怎么样做才能调用一个控制台应用程序并将参数传递过去,控制台程序执行完后,我们的程序又怎样获取返回值?代码如下:调用代码:    ...

  6. c#Winform程序调用app.config文件配置数据库连接字符串

    你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings name="  " connectionString= ...

  7. winform 程序调用及参数调用

    调用程序: // 新轮廓 -> 调用轮廓扫描程序        private void toolStripMenuItem9_Click(object sender, EventArgs e) ...

  8. 从web页面启动winform程序的实现方法

    本文实现的需求是: A.通过web页面启动winform程序: B.将页面的参数传递给winform程序: C.winform程序已经启动并正在运行时,从web页面不能重新启动winform程序,只是 ...

  9. 在WinForm中使用Web Service来实现软件自动升级

    来源:互联网 winform程序相对web程序而言,功能更强大编程更方便,但软件更新却相当麻烦,要到客户端一台一台地升级,面对这个实际问题,在最近的一个小项目中,本人设计了一个通过软件实现自动升级技术 ...

随机推荐

  1. 使用HTML5 WebDataBase设计离线数据库

    基于HTML5的Web DataBase 可以让你在浏览器中进行数据持久地存储管理和有效查询,假设你的离线应用程序有需要规范化的存储功能,那么使用Web DataBase,可以使你的应用程序无论是在离 ...

  2. Android开发--WIFI实现

    wifi的基本结构 在Android的官方文档中定义了如下五种状态: WIFI_STATE_DISABLING  WIFI网卡正在关闭  0 WIFI_STATE_DISABLED   WIFI网卡不 ...

  3. 【开源java游戏框架libgdx专题】-05-模块描述与上下文

    模块描述(Modules overview) Input:为所有的平台提供一个统一的输入模型和处理程序. 获取触摸示例: if (Gdx.input.isTouched()) { System.out ...

  4. Cacti以MB为单位监控流量

    Cacti自带的流量监控阀值模板为“Interface – Traffic”,只能监控bytes,在添加阀值之后,报警的流量信息以bytes为单位,查看很不友好,可以通过以下方法将btyes转换成MB ...

  5. SQL批量信息保存(XML格式字符串数据)

    /* *功能:SQL批量信息录入 *此存储过程获取表单信息,插入表中.*/CREATE  PROC [dbo].[sp_SaveToMX1]@XML text   --明细表XML字符串信息ASBEG ...

  6. nodejs原生模块简介

    一.Express框架 前面的章节已经介绍过了,可以使用npm来安装node.js模块.具体操作请参照以前写的nodejs概论. Express是一个nodejs的web开源框架,用于快速的搭建web ...

  7. JavaScript_object基础

    之前写Java时老是有点蒙,大部分都是用jQuery,但原理还不是很清楚,最近一段时间在系统的学习JavaScript,有什么问题或错误请指出,多谢..................... Obje ...

  8. Pythonchallenge一起来闯关

    http://www.pythonchallenge.com/是一个在线的python过关游戏,一共有33关.玩这个游戏对熟悉python用法及相关库的使用都很有好处. 目前做到了第九关.python ...

  9. 【转】从头到尾彻底理解KMP

    很好,讲得很清晰,值得学习. 作者:July时间:最初写于2011年12月,2014年7月21日晚10点 全部删除重写成此文,随后的半个月从早到晚不断改进. 1. 引言 本KMP原文最初写于2年多前的 ...

  10. Unable to locate package错误解决办法

    新装了VMWare Player,结果装上Ubuntu12.04后安装软件都提示:Unable to locate package错误,解决方法非常简单,终端输入以下命令即可: sudo apt-ge ...