关于WebService的调用

国内网站地址:
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx

英文网站地址:
http://www.webservicex.net/globalWeather.asmx

 

 第一种方法:用wsdl命令;调用英文网站。

第一步:在命令提示符下输入
C:\>cd C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin

第二步:输入
wsdl /language:c# /n:wsdlLib /out:d:\cc\TestService.cs  http://www.webservicex.net/globalWeather.asmx?wsdl

wsdl /language:VB /n:wsdlLib /out:d:\cc\TestService.bas  http://www.webservicex.net/globalWeather.asmx?wsdl

 

参数说明:
/language:语言
/n:命名空间
/out:输入文件路径和文件名
(注:http://www.webservicex.net/globalWeather.asmx 生成的服务地址)

本实例测试用C#、VB调用成功,调用成功后分别生成d:\cc\TestService.cs和d:\cc\TestService.bas

 

 第二种方法:用菜单命令add-->web referenes;调用国内网站。

(1)WebService工程的代码如下:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace WebService
{
    /// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]//必须要有的,为了说明,其下是一个方法,每一个方法前面都需要有。
        public string getName(string byvName)
        {
            return byvName;
        }

        [WebMethod]
        public string getAge()
        {
            return "25";
        }

    }
}

 (2)WindowsFormsApplication1工程的代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
        }
        ServiceReference2.WeatherWebServiceSoapClient w = new WindowsFormsApplication1.ServiceReference2.WeatherWebServiceSoapClient("WeatherWebServiceSoap");
       
        private void Form1_Load(object sender, EventArgs e)
        {
            ServiceReference1.Service1SoapClient s = new WindowsFormsApplication1.ServiceReference1.Service1SoapClient();
            this.label1.Text = s.HelloWorld();
            this.label2.Text = s.getAge();

            cbProvince.DataSource = w.getSupportProvince();
        }
        private void cbProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbCity.DataSource = w.getSupportCity(cbProvince.Text.Trim());
        }

        private void cbCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            string cityName = cbCity.Text.Trim();
            string cName = cityName.Split(new char[] { '(' })[0]; 
           
            string[] ws = new string[22];
            string temp = string.Empty;

            ws = w.getWeatherbyCityName(cName);
            for (int i = 0; i <= ws.Length -1; i++)
            {
                temp += ws[i].ToString() + "\\";
            }
            rtbWeather.Text = temp;
            listBox1.DataSource = ws;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            //this.Close();
            Application.Exit();
        }
       
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值