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     : 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的值改为实际的值;

内容概要:本文围绕可变桨叶四旋翼无人机的规范控制与点对点运动模拟展开,重点研究优化推力分配策略在翻转动作中的应用与性能比较。通过Matlab代码实现,构建了四旋翼动力学模型,并设计了多种控制算法以实现精确的姿态调整与轨迹跟踪。研究对比了不同推力分配方案在执行高机动性翻转动作时的稳定性、能耗效率与响应速度,旨在提升无人机在复杂飞行任务中的动态性能与控制精度。该仿真研究为无人机飞控系统的设计与优化提供了理论依据技术支持。; 适合人群:具备定自动控制理论基础Matlab编程能力,从事无人机控制、飞行器动力学或机器人系统研究的科研人员及研究生。; 使用场景及目标:① 实现四旋翼无人机在三维空间中的精确点对点运动控制;② 对比分析不同推力分配策略在执行翻转等高难度动作时的控制效果与能耗表现,优化飞行性能;③ 为无人机自主飞行、特技飞行及复杂环境下的机动控制提供算法验证平台。; 阅读建议:此资源以Matlab仿真为核心,建议读者结合相关控制理论知识,深入理解代码实现细节,重点关注动力学建模、控制律设计与推力分配模块。在学习过程中,应动手调试参数,复现文中翻转动作的仿真结果,并尝试拓展至其他复杂飞行任务,以加深对无人机控制机理的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值