国内网站地址:
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();
}
}
}
![](http://hi.csdn.net/attachment/201108/4/0_1312424923q0qU.gif)