一:用ASP.NET调用Web Service

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

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

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

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

        hong.Service1SoapClient login = new hong.Service1SoapClient();if (txtName.Text.Trim().Length == 0){MessageBox.Show("请输入用户名!");return;}else if (pwdInfo.Password.Trim().Length == 0){MessageBox.Show("请输入密码!");return;}

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

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server"><title>Webservice调用实例</title>
</head>
<body><form id="form2" runat="server"><div><asp:TextBox ID="Num1" runat="server"></asp:TextBox><select id="selectOper" runat = "server"><option>+</option><option>-</option><option>*</option><option>/</option></select><asp:TextBox ID="Num2" runat="server"></asp:TextBox><span id = E runat = "server"></span><asp:TextBox ID="Result" runat="server"></asp:TextBox></div>
</form>
</body>
</html>

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

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace WebApplication2
{public partial class _Default : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){//在页面加载的时候动态创建一个按钮,在它的事件里调用WebserviceButton btn = new Button();btn.Width = 20;btn.Text = " = ";btn.Click += new EventHandler(btn_Click);E.Controls.Add(btn);}/// <summary>/// 定义动态创建Button的Click事件,在这个事件中调用Webservice/// </summary>/// <param name="sender"></param>/// <param name="e"></param>void btn_Click(object sender, EventArgs e){if (Num1.Text != "" && Num2.Text != ""){//实例化引用的webservice对象localhost.Service1 WebserviceInstance = new localhost.Service1();int Oper = selectOper.SelectedIndex;switch (Oper){//通过实例化的webservice对象来调用Webservice暴露的方法case 0:Result.Text = WebserviceInstance.addition(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();break;case 1:Result.Text = WebserviceInstance.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();break;case 2:Result.Text = WebserviceInstance.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();break;case 3:Result.Text = WebserviceInstance.division(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();break;}}}}
}

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

其中的报错解决

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

二:winform程序调用Web Service

运行效果图:

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

e

界面设计代码:

namespace winform
{partial class Form1{/// <summary>/// 必需的设计器变量。/// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// 清理所有正在使用的资源。/// </summary>/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。/// </summary>private void InitializeComponent(){this.btnCompute = new System.Windows.Forms.Button();this.tbNumX = new System.Windows.Forms.TextBox();this.tbNumY = new System.Windows.Forms.TextBox();this.tbResult = new System.Windows.Forms.TextBox();this.label2 = new System.Windows.Forms.Label();this.cmbWays = new System.Windows.Forms.ComboBox();this.SuspendLayout();// // btnCompute// this.btnCompute.Location = new System.Drawing.Point(212, 90);this.btnCompute.Name = "btnCompute";this.btnCompute.Size = new System.Drawing.Size(75, 23);this.btnCompute.TabIndex = 0;this.btnCompute.Text = "计算";this.btnCompute.UseVisualStyleBackColor = true;this.btnCompute.Click += new System.EventHandler(this.btnCompute_Click);// // tbNumX// this.tbNumX.Location = new System.Drawing.Point(41, 40);this.tbNumX.Name = "tbNumX";this.tbNumX.Size = new System.Drawing.Size(100, 21);this.tbNumX.TabIndex = 1;// // tbNumY// this.tbNumY.Location = new System.Drawing.Point(222, 39);this.tbNumY.Name = "tbNumY";this.tbNumY.Size = new System.Drawing.Size(100, 21);this.tbNumY.TabIndex = 2;// // tbResult// this.tbResult.Location = new System.Drawing.Point(349, 39);this.tbResult.Name = "tbResult";this.tbResult.Size = new System.Drawing.Size(100, 21);this.tbResult.TabIndex = 3;// // label2// this.label2.AutoSize = true;this.label2.Location = new System.Drawing.Point(330, 43);this.label2.Name = "label2";this.label2.Size = new System.Drawing.Size(11, 12);this.label2.TabIndex = 5;this.label2.Text = "=";// // cmbWays// this.cmbWays.FormattingEnabled = true;this.cmbWays.Location = new System.Drawing.Point(149, 40);this.cmbWays.Name = "cmbWays";this.cmbWays.Size = new System.Drawing.Size(65, 20);this.cmbWays.TabIndex = 6;// // Form1// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(471, 131);this.Controls.Add(this.cmbWays);this.Controls.Add(this.label2);this.Controls.Add(this.tbResult);this.Controls.Add(this.tbNumY);this.Controls.Add(this.tbNumX);this.Controls.Add(this.btnCompute);this.Name = "Form1";this.Text = "Form1";this.Load += new System.EventHandler(this.Form1_Load);this.ResumeLayout(false);this.PerformLayout();}#endregionprivate System.Windows.Forms.Button btnCompute;private System.Windows.Forms.TextBox tbNumX;private System.Windows.Forms.TextBox tbNumY;private System.Windows.Forms.TextBox tbResult;private System.Windows.Forms.Label label2;private System.Windows.Forms.ComboBox cmbWays;}
}

后台代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace winform
{public partial class Form1 : Form{//Sign s = new Sign();public Form1(){InitializeComponent();}private void btnCompute_Click(object sender, EventArgs e){if (tbNumX.Text != "" && tbNumY.Text != ""){//实例化引用的webservice对象Compute.Service1 WebserviceInstance = new Compute.Service1 ();int a =Convert.ToInt32( cmbWays.SelectedItem.ToString().Trim().Substring(0,1));switch (a){//通过实例化的webservice对象来调用Webservice暴露的方法case 0:tbResult.Text = WebserviceInstance.addition(double.Parse(tbNumX.Text), double.Parse(tbNumY.Text)).ToString();break;case 1:tbResult.Text = WebserviceInstance.subtract(double.Parse(tbNumX.Text), double.Parse(tbNumY.Text)).ToString();break;case 2:tbResult.Text = WebserviceInstance.multiplication(double.Parse(tbNumX.Text), double.Parse(tbNumY.Text)).ToString();break;case 3:tbResult.Text = WebserviceInstance.division(double.Parse(tbNumX.Text), double.Parse(tbNumY.Text)).ToString();break;}}}//载入加减乘除的符号private void Form1_Load(object sender, EventArgs e){Sign add = new Sign { Name = "+", Num = 0 };Sign sub = new Sign { Name = "-", Num = 1 };Sign mul = new Sign { Name = "*", Num = 2 };Sign div = new Sign { Name = "/", Num = 3 };//cmbWays.Items.Add(add);
cmbWays.Items.Add(add.Num+"---> "+ add.Name);cmbWays.Items.Add(sub.Num + "---> " + sub.Name);cmbWays.Items.Add(mul.Num + "---> " + mul.Name);cmbWays.Items.Add(div.Num + "---> " + div.Name);cmbWays.SelectedItem = add.Num + "---> " + add.Name;//去掉了空格,默认选择项
}}
}

自定义类:

using System;
using System.Collections.Generic;
using System.Text;namespace winform
{public class Sign{public int Num { get; set; }public string Name { get; set; }}
}

相关的文章还有:

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

转载于:https://www.cnblogs.com/hongmaju/p/4024053.html

C#之VS2010ASP.NET页面调用Web Service和winform程序调用Web Service相关推荐

  1. react组件卸载调用的方法_好程序员web前端培训分享React学习笔记(三)

    好程序员web前端培训分享React学习笔记(三),组件的生命周期 React中组件也有生命周期,也就是说也有很多钩子函数供我们使用, 组件的生命周期,我们会分为四个阶段,初始化.运行中.销毁.错误处 ...

  2. 微信小程序调用PHP接口,微信小程序调用PHP后台接口教程

    微信小程序调用PHP后台接口,解析纯html文本,效果图片预览 1.微信js动态传参: wx.request({ url: 'https://m.****.com/index.php/Home/Xia ...

  3. vlc集成c#_C#WinForm程序调用VLC异常

    问题描述 使用WinForm程序,调用VLC初始化实例时报异常,就是运行到libvlc_new(arguments.Length, argvPtr)方法时报的异常 异常消息:System.BadIma ...

  4. web加android开发程序吗,Web与Android真正的结合

    "纸飞机",刚开始只是一个简单的想法- "能从一个屏幕扔个纸飞机到另一个屏幕" 提出这个想法人的核心理念是利用网络的力量(即时通讯)把到世界各地所有人连接到一起 ...

  5. java 调用cmd_java打开本地应用程序(调用cmd)---Runtime用法详解

    有时候我们需要借助java程序打开电脑自带的一些程序,可以直接打开或者借助cmd命令窗口打开一些常用的应用程序或者脚本,在cmd窗口执行的命令都可以通过这种方式运行. 例如: packagecn.xm ...

  6. C# winform程序调用Console控制台办法

    using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; usi ...

  7. c# winform程序调用托管dll(c#的dll),使用添加引用和动态加载dll

    1. dll要强名. 2. winform程序要添加引用dll(自动获得dll的函数调用定义),"复制本地"属性设false,因为下一步会动态加载dll,所以"复制本地& ...

  8. linux下c代码调用.so,Linux下C程序调用.so(动态链)的一个例子

    /********************************************** * 使用Linux下C++的动态链接库.So文件 * void *pdlHandle; * pdlHan ...

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

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

最新文章

  1. Python:UTF-8编码转换成GBK编码
  2. 深入实践Spring Boot2.3.2 文档建模
  3. webpack把源代码里面的console和debugger全部去掉_在webpack中,module、chunk和bundle到底是什么样的存在?...
  4. 2019 校招 ,下一个“神”人,在哪里?
  5. C语言写300k文件大概多少行,为什么 DELPHI 编译出的程序一般的来说至少都有300k呢?...
  6. c语言子线程给主线程发信息,如何用C语言实现多线程
  7. python的目的_Python-** wargs的目的和用途是什么?
  8. Elasticsearch Java Low Level REST Client(读取响应)
  9. unix 网络编程总结
  10. Atitti.java android反编译解决方案-----虚拟机方案
  11. Vue3,Vite3,TS,Naive-UI整合TailwindCSS
  12. 如何利用Gmail群发电子邮件
  13. 三进制 四进制计算机原理,三进制计算机(中国三进制计算机)
  14. 视频学习前端的经验之谈
  15. Deepin系统初体验指南:从安装到开发环境配置
  16. linux编辑搜索命令,Linux 命令大全提供 500 多个 Linux 命令搜索
  17. Pytorch实现Bert模型
  18. 罗德里格旋转公式推导(自制)
  19. tomcat重启机制
  20. 东华大学 oj1——求长方形的面积和周长

热门文章

  1. QCC51XX---无线调试
  2. Xshell通过ssh上传文件到服务器
  3. Vue全局变量使用与修改
  4. SVN database disk image is maiformed
  5. GitHub Page个人博客中评论功能
  6. Android中的sdk相关介绍
  7. Densepose安装教程
  8. flex和php,开始使用flex和weborb对于PHP
  9. 违约预测模型后续工作
  10. SCI或EI投稿经验