一:用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 == )
{
MessageBox.Show("请输入用户名!");
return;
}
else if (pwdInfo.Password.Trim().Length == )
{
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)
{ //在页面加载的时候动态创建一个按钮,在它的事件里调用Webservice
Button btn = new Button();
btn.Width = ;
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 :
Result.Text = WebserviceInstance.addition(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
break;
case :
Result.Text = WebserviceInstance.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
break;
case :
Result.Text = WebserviceInstance.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text)).ToString();
break;
case :
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(, );
this.btnCompute.Name = "btnCompute";
this.btnCompute.Size = new System.Drawing.Size(, );
this.btnCompute.TabIndex = ;
this.btnCompute.Text = "计算";
this.btnCompute.UseVisualStyleBackColor = true;
this.btnCompute.Click += new System.EventHandler(this.btnCompute_Click);
//
// tbNumX
//
this.tbNumX.Location = new System.Drawing.Point(, );
this.tbNumX.Name = "tbNumX";
this.tbNumX.Size = new System.Drawing.Size(, );
this.tbNumX.TabIndex = ;
//
// tbNumY
//
this.tbNumY.Location = new System.Drawing.Point(, );
this.tbNumY.Name = "tbNumY";
this.tbNumY.Size = new System.Drawing.Size(, );
this.tbNumY.TabIndex = ;
//
// tbResult
//
this.tbResult.Location = new System.Drawing.Point(, );
this.tbResult.Name = "tbResult";
this.tbResult.Size = new System.Drawing.Size(, );
this.tbResult.TabIndex = ;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(, );
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(, );
this.label2.TabIndex = ;
this.label2.Text = "=";
//
// cmbWays
//
this.cmbWays.FormattingEnabled = true;
this.cmbWays.Location = new System.Drawing.Point(, );
this.cmbWays.Name = "cmbWays";
this.cmbWays.Size = new System.Drawing.Size(, );
this.cmbWays.TabIndex = ;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(, );
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(); } #endregion private 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(,)); switch (a)
{
//通过实例化的webservice对象来调用Webservice暴露的方法
case :
tbResult.Text = WebserviceInstance.addition(double.Parse(tbNumX.Text), double.Parse(tbNumY.Text)).ToString();
break;
case :
tbResult.Text = WebserviceInstance.subtract(double.Parse(tbNumX.Text), double.Parse(tbNumY.Text)).ToString();
break;
case :
tbResult.Text = WebserviceInstance.multiplication(double.Parse(tbNumX.Text), double.Parse(tbNumY.Text)).ToString();
break;
case :
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 = };
Sign sub = new Sign { Name = "-", Num = };
Sign mul = new Sign { Name = "*", Num = };
Sign div = new Sign { Name = "/", Num = }; //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实现天气预报

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. spring 中StoredProcedure的用法--转载

    StoredProcedure是一个抽象类,必须写一个子类来继承它,这个类是用来简化JDBCTemplate执行存储过程操作的. 首先我们写一个实现类: package com.huaye.frame ...

  2. ld: 18 duplicate symbols for architecture i386 .linker command failed with exit code 1 (use -v to see invocation)_

    昨天被linker这个错误卡了一个小时!!!各种办法都试了 是导入第三方的问题 .. 网上说 要把所有的.m文件导入  但是我下载的微博SDK根本不关事..后来 大概知道是导入了多个相同的文件... ...

  3. Android-SVN

    服务器启动svn服务 svnserve -d -r /home/wbp/svn/actia/ 1 .svn  重新定位location , 改变新仓库的uuid , 今天操作SVN Client 发现 ...

  4. 带A圈的秘密

    真嗒安全策略的罗罗,,害的我和其他的不一样.

  5. RSA PKCS1padding 填充模式

    在BouncyCastle实现RSA的PKCS1V1.5模式中,如果是公钥加密信息(forEncryption=true),密钥长度为1024位,那么输出的密文块长度为128个字节,输入的明文块长度为 ...

  6. openMPI小集群安装

    经过一天的努力,终于完成了openMPI的多节点安装,即小集群安装.本文使用的是openmpi-1.6.5,下载地址见:http://www.open-mpi.org/software/ompi/v1 ...

  7. underscorejs-reduce学习

    2.3 reduce 2.3.1 语法: _.reduce(list, iteratee, [memo], [context]) 2.3.2 说明: reduce方法把list中元素归结为一个单独的数 ...

  8. 21 Merge Two Sorted Lists(两链表归并排序Easy)

    题目意思:对两个递增链表进行归并排序 思路:没什么好说的,二路归并 /** * Definition for singly-linked list. * struct ListNode { * int ...

  9. 136 Single Number(找唯一数Medium)

    题目意思:一个int数组,有一个数只出现一次,其他数均出现两次,找到这个唯一数 知识普及:~:非运算,单目运算符1为0,0为1;   &:与运算,都为1则为1,否则为0 |:或运算,全为0则为 ...

  10. java使用json抛出org.apache.commons.lang.exception.NestableRuntimeException解决方案

    出现这个问题,说明缺少jar包,将下面的jar引入即可 commons-beanutils-1.8.3 commons-lang-2.6 (注:导入最新的 3.1 版本会继续报如下错误) common ...