加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

关于WebService的调用

发布时间:2020-12-17 01:15:19 所属栏目:安全 来源:网络整理
导读:国内网站地址: http://www.webxml.com.cn/WebServices/WeatherWebService.asmx 英文网站地址: http://www.webservicex.net/globalWeather.asmx ? ?第一种方法:用wsdl命令;调用英文网站。 第一步:在命令提示符下输入 C:cd C:Program FilesMicrosoft S

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

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

?

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

第一步:在命令提示符下输入
C:&;cd C:Program FilesMicrosoft SDKsWindowsv6.0ABin

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

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

?

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

本实例测试用C#、VB调用成功,调用成功后分别生成d:ccTestService.cs和d:ccTestService.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();
??????? }
???????
??? }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读