您的位置:首页 > 编程语言 > Delphi

Delphi 编写调用WebService接口的小程序(天气预报)

2012-09-13 16:31 501 查看
Delphi 编写调用WebService接口的小程序

最近由于项目需要在编写一个webservice的客户端,网上找来了不少资料,都说可以通过delphi自带的向导来生成调用接口的文件。

如对天气预报的webservice调用http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl ,查看各地的天气预报

1、打开delphi开发软件到 File--> New--> Application 新建一个工程,在窗体上放置两个 Button按钮,一个ComboBox,一个Edit

一个Memo和一个HTTPRIO1



2、到File--> New--> Other--> WebServices--> WSDL importer



用WSDL Import Wizard 窗口,在Location of WSDL File or URL中填写天气预报的wsdl路径http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl 后点击Next按钮,再点击finish按钮,这样delphi就自动把接口描述的xml文档生成了可以调用的单元文件WeatherWebService.pas如下

// ************************************************************************ //

// The types declared in this file were generated from data read from the

// WSDL File described below:

// WSDL : http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
// Encoding : utf-8

// Version : 1.0

// (2012-9-13 9:58:02 - 1.33.2.5)

// ************************************************************************ //

unit WeatherWebService;

interface

uses InvokeRegistry, SOAPHTTPClient, Types, XSBuiltIns;

type

// ************************************************************************ //

// The following types, referred to in the WSDL document are not being represented

// in this file. They are either aliases[@] of other types represented or were referred

// to but never[!] declared in the document. The types from the latter category

// typically map to predefined/known XML or Borland types; however, they could also

// indicate incorrect WSDL documents that failed to declare or import a schema type.

// ************************************************************************ //

// !:string - "http://www.w3.org/2001/XMLSchema"

getSupportDataSetResult = class; { "http://WebXml.com.cn/" }

ArrayOfString = array of WideString; { "http://WebXml.com.cn/" }

// ************************************************************************ //

// Namespace : http://WebXml.com.cn/

// ************************************************************************ //

getSupportDataSetResult = class(TRemotable)

private

Fschema: WideString;

published

property schema: WideString read Fschema write Fschema;

end;

// ************************************************************************ //

// Namespace : http://WebXml.com.cn/

// soapAction: http://WebXml.com.cn/%operationName%

// transport : http://schemas.xmlsoap.org/soap/http

// binding : WeatherWebServiceSoap

// service : WeatherWebService

// port : WeatherWebServiceSoap

// URL : http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
// ************************************************************************ //

WeatherWebServiceSoap = interface(IInvokable)

['{0AF62441-3FA0-F5D8-B6B8-B486F32F9DDE}']

function getSupportCity(const byProvinceName: WideString): ArrayOfString; stdcall;

function getSupportProvince: ArrayOfString; stdcall;

function getSupportDataSet: getSupportDataSetResult; stdcall;

function getWeatherbyCityName(const theCityName: WideString): ArrayOfString; stdcall;

function getWeatherbyCityNamePro(const theCityName: WideString; const theUserID: WideString): ArrayOfString; stdcall;

end;

function GetWeatherWebServiceSoap(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): WeatherWebServiceSoap;

implementation

function GetWeatherWebServiceSoap(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): WeatherWebServiceSoap;

const

defWSDL = 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl';

defURL = 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx';

defSvc = 'WeatherWebService';

defPrt = 'WeatherWebServiceSoap';

var

RIO: THTTPRIO;

begin

Result := nil;

if (Addr = '') then

begin

if UseWSDL then

Addr := defWSDL

else

Addr := defURL;

end;

if HTTPRIO = nil then

RIO := THTTPRIO.Create(nil)

else

RIO := HTTPRIO;

try

Result := (RIO as WeatherWebServiceSoap);

if UseWSDL then

begin

RIO.WSDLLocation := Addr;

RIO.Service := defSvc;

RIO.Port := defPrt;

end else

RIO.URL := Addr;

finally

if (Result = nil) and (HTTPRIO = nil) then

RIO.Free;

end;

end;

initialization

InvRegistry.RegisterInterface(TypeInfo(WeatherWebServiceSoap), 'http://WebXml.com.cn/', 'utf-8');

InvRegistry.RegisterDefaultSOAPAction(TypeInfo(WeatherWebServiceSoap), 'http://WebXml.com.cn/%operationName%');

InvRegistry.RegisterInvokeOptions(TypeInfo(WeatherWebServiceSoap), ioDocument);//遇到调用城市调用错误后增加的

RemClassRegistry.RegisterXSInfo(TypeInfo(ArrayOfString), 'http://WebXml.com.cn/', 'ArrayOfString');

RemClassRegistry.RegisterXSClass(getSupportDataSetResult, 'http://WebXml.com.cn/', 'getSupportDataSetResult');

end.

在主单元中添加代码

procedure TForm1.btn1Click(Sender: TObject);//获取支持的省份

var

pras,city:ArrayOfString;

i,j:Integer;

begin

pras := (HTTPRIO1 as WeatherWebServiceSoap).getSupportProvince;

for i:= Low(pras) to High(pras) do

begin

cbb1.Items.Add(pras[i]);

end;

end;

procedure TForm1.btn2Click(Sender: TObject);//获取某个城市的天气 edt1中填写城市名称如"杭州"

var

cityweather:ArrayOfString;

t:string;

i:Integer;

begin

cityweather := (HTTPRIO1 as WeatherWebServiceSoap).getWeatherbyCityName(edt1.Text);

for i:= Low(cityweather) to High(cityweather) do

begin

t:= cityweather[i];

mmo1.Lines.Add(t);

end;

end;

但是在调用的过程中,当调用城市天气的时候getWeatherbyCityName出现下面的错误



在网上找了资料,根据以下的修改后不会出现错误,但由于初次接触webservice不清楚到底修改了有什么用?如果有人知道希望说个所以然。

1. 把 HTTPRIO1-> HTTPWebNode->的UseUTF8InHeader 设为True

2. 打开WeatherWebService.pas在initialization最下面添加上

InvRegistry.RegisterInvokeOptions(TypeInfo(WeatherWebServiceSoap), ioDocument);

修改完以后,编译工程,就可以调用看到相关的信息了





由于对方提供的wsdl文档发布在专网内,公网访问不到,所以用上面的向导方法无法生成我需要的文件,还要另找方法

1、先到能够访问到WSDL文档的计算机上,访问提供的URL,然后在跳出的URL界面上右键“查看源文件”,将会以记事本形式打开XML文档,把此文档保存下来,后缀改为“.XML”。

2、在向导窗口

location of WSDL File URL的下拉框的右边又一个按钮,点击此按钮出现文件打开的窗口

,找到刚才保存的XML文件,打开然后点击“Next”,“finish”也生成了需要的单元文件。但是实际应用中需要把单元文件中的常量defWSDL的值改为实际的值;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐