Delphi 編寫呼叫WebService介面的小程式(天氣預報)
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 :
// 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 - "
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的值改為實際的值;