Delphi7 客户端调用WebService(天气预报)

摘要:
其他------->Web服务---->程序TForm1。按钮单击(发件人:ArrayOfString;//石家庄备忘录1。线清除=0到22确实开始Memo1。线添加(TMP_Weather[i]);终止

客户程序:
第一步:新建一个Application

第二步:File----->New----->Other------>WebServices----->WSDL Importer

       Delphi7 客户端调用WebService(天气预报)第1张

 

然后在Location of WSDL File or URL中填入: 

http://10.22.30.61:36601/MonitorService.asmx?wsdl

或 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl,然后确定即生成了一个新的接口定义单元。

第二步:在主form上放上一个按钮和一个Httprio组件(在WebServices页上),并引用第二个单元(即通过WSDL Importer自动生成的单元)

Httprio的属性页上的WsdlLocation里面填上http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl;然后在Httprio属性页上的PortService上选择上相应的数据即可。

第三步:书写客户调用程序,原代码如下:

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  TMP_Weather: ArrayOfString;
begin
  //http://developer.51cto.com/art/200908/147125.htm
  //http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
  TMP_Weather := (HTTPRIO1 as WeatherWebServiceSoap).getWeatherbyCityName('53698'); //石家庄

  Memo1.Lines.Clear;
  for i := 0 to 22 do
  begin
    Memo1.Lines.Add(TMP_Weather[i]);
  end;
end;

---- 错误信息为:
Project Project1.exe raised exception class ERemotableException with message '服务器无法处理请求。 ---> 未将对象引用设置到对象的实例。'. Process stopped. Use Step or Run to continue. 

------解决方案--------------------------------------------------------
修改一下WeatherWebService.pas这个单元,如下所示,一切就OK啦。

Delphi(Pascal) code

  if HTTPRIO = nil then    RIO := THTTPRIO.Create(nil)  else    RIO := HTTPRIO;     RIO.HTTPWebNode.UseUTF8InHeader:= True;  //这里加上这一句  try    Result := (RIO as TestStationSoap);    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;    InvRegistry.RegisterInterface(TypeInfo(WeatherWebServiceSoap), 'http://WebXml.com.cn/', 'utf-8');  InvRegistry.RegisterDefaultSOAPAction(TypeInfo(WeatherWebServiceSoap), 'http://WebXml.com.cn/%operationName%');  RemClassRegistry.RegisterXSInfo(TypeInfo(ArrayOfString), 'http://WebXml.com.cn/', 'ArrayOfString');  RemClassRegistry.RegisterXSClass(getSupportDataSetResult, 'http://WebXml.com.cn/', 'getSupportDataSetResult');    InvRegistry.RegisterInvokeOptions(TypeInfo(WeatherWebServiceSoap), ioDocument);//这里加上这一句------解决方案--------------------------------------------------------补充一下,如果你用到HTTPRIO1去调webservice接口那就记得在前面加上这句HTTPRIO1.HTTPWebNode.UseUTF8InHeader:= True;//解决汉字乱码问题

本文出自:http://blog.csdn.net/gjtao1130/article/details/12193235  只是做部分修正。

//-------------以下是本人模仿的例子,窗体没有用到Httprio组件,通过GetWeatherWebServiceSoap函数处理即可。

Delphi7 客户端调用WebService(天气预报)第2张Delphi7 客户端调用WebService(天气预报)第3张
  1 // ************************************************************************ //
  2 // The types declared in this file were generated from data read from the
  3 // WSDL File described below:
  4 // WSDL     : http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
  5 // Encoding : utf-8
  6 // Version  : 1.0
  7 // (2014/12/10 11:28:28 - 1.33.2.5)
  8 // ************************************************************************ //
  9 
 10 unit WeatherWebService;
 11 
 12 interface
 13 
 14 uses InvokeRegistry, SOAPHTTPClient, Types, XSBuiltIns;
 15 
 16 type
 17 
 18   // ************************************************************************ //
 19   // The following types, referred to in the WSDL document are not being represented
 20   // in this file. They are either aliases[@] of other types represented or were referred
 21   // to but never[!] declared in the document. The types from the latter category
 22   // typically map to predefined/known XML or Borland types; however, they could also 
 23   // indicate incorrect WSDL documents that failed to declare or import a schema type.
 24   // ************************************************************************ //
 25   // !:string          - "http://www.w3.org/2001/XMLSchema"
 26 
 27   getSupportDataSetResult = class;              { "http://WebXml.com.cn/" }
 28 
 29   ArrayOfString = array of WideString;          { "http://WebXml.com.cn/" }
 30 
 31 
 32   // ************************************************************************ //
 33   // Namespace : http://WebXml.com.cn/
 34   // ************************************************************************ //
 35   getSupportDataSetResult = class(TRemotable)
 36   private
 37     Fschema: WideString;
 38   published
 39     property schema: WideString read Fschema write Fschema;
 40   end;
 41 
 42 
 43   // ************************************************************************ //
 44   // Namespace : http://WebXml.com.cn/
 45   // soapAction: http://WebXml.com.cn/%operationName%
 46   // transport : http://schemas.xmlsoap.org/soap/http
 47   // binding   : WeatherWebServiceSoap
 48   // service   : WeatherWebService
 49   // port      : WeatherWebServiceSoap
 50   // URL       : http://www.webxml.com.cn/WebServices/WeatherWebService.asmx
 51   // ************************************************************************ //
 52   WeatherWebServiceSoap = interface(IInvokable)
 53   ['{0AF62441-3FA0-F5D8-B6B8-B486F32F9DDE}']
 54     function  getSupportCity(const byProvinceName: WideString): ArrayOfString; stdcall;
 55     function  getSupportProvince: ArrayOfString; stdcall;
 56     function  getSupportDataSet: getSupportDataSetResult; stdcall;
 57     function  getWeatherbyCityName(const theCityName: WideString): ArrayOfString; stdcall;
 58     function  getWeatherbyCityNamePro(const theCityName: WideString; const theUserID: WideString): ArrayOfString; stdcall;
 59   end;
 60 
 61 function GetWeatherWebServiceSoap(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): WeatherWebServiceSoap;
 62 
 63 
 64 implementation
 65 
 66 function GetWeatherWebServiceSoap(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): WeatherWebServiceSoap;
 67 const
 68   defWSDL = 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl';
 69   defURL  = 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx';
 70   defSvc  = 'WeatherWebService';
 71   defPrt  = 'WeatherWebServiceSoap';
 72 var
 73   RIO: THTTPRIO;
 74 begin
 75   Result := nil;
 76   if (Addr = '') then
 77   begin
 78     if UseWSDL then
 79       Addr := defWSDL
 80     else
 81       Addr := defURL;
 82   end;
 83   if HTTPRIO = nil then
 84     RIO := THTTPRIO.Create(nil)
 85   else
 86     RIO := HTTPRIO;
 87   RIO.HTTPWebNode.UseUTF8InHeader:= True;  //这里加上这一句
 88   try
 89     Result := (RIO as WeatherWebServiceSoap);
 90     if UseWSDL then
 91     begin
 92       RIO.WSDLLocation := Addr;
 93       RIO.Service := defSvc;
 94       RIO.Port := defPrt;
 95     end else
 96       RIO.URL := Addr;
 97   finally
 98     if (Result = nil) and (HTTPRIO = nil) then
 99       RIO.Free;
100   end;
101 end;
102 
103 
104 initialization
105   InvRegistry.RegisterInterface(TypeInfo(WeatherWebServiceSoap), 'http://WebXml.com.cn/', 'utf-8');
106   InvRegistry.RegisterDefaultSOAPAction(TypeInfo(WeatherWebServiceSoap), 'http://WebXml.com.cn/%operationName%');
107   RemClassRegistry.RegisterXSInfo(TypeInfo(ArrayOfString), 'http://WebXml.com.cn/', 'ArrayOfString');
108   RemClassRegistry.RegisterXSClass(getSupportDataSetResult, 'http://WebXml.com.cn/', 'getSupportDataSetResult');
109   InvRegistry.RegisterInvokeOptions(TypeInfo(WeatherWebServiceSoap), ioDocument);//这里加上这一句
110 
111 end.
自动生成的WebService文件
Delphi7 客户端调用WebService(天气预报)第2张Delphi7 客户端调用WebService(天气预报)第5张
 1 unit Unit1;
 2 
 3 interface
 4 
 5 uses
 6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7   Dialogs, StdCtrls, ExtCtrls;//, InvokeRegistry, Rio, SOAPHTTPClient;
 8 
 9 type
10   TForm1 = class(TForm)
11     mmo1: TMemo;
12     pnl1: TPanel;
13     btn1: TButton;
14     procedure btn1Click(Sender: TObject);
15   private
16     { Private declarations }
17   public
18     { Public declarations }
19   end;
20 
21 var
22   Form1: TForm1;
23 
24 implementation
25 
26 {$R *.dfm}
27  //网页例子 http://blog.csdn.net/gjtao1130/article/details/12193235
28 uses WeatherWebService;
29 
30 procedure TForm1.btn1Click(Sender: TObject);
31 var
32   i: Integer;
33   TMP_Weather: ArrayOfString;
34 begin
35   //http://developer.51cto.com/art/200908/147125.htm
36   //http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
37   {TMP_Weather := (htpr1 as WeatherWebServiceSoap).getSupportCity('广西');//玉林 (59453)
38   mmo1.Lines.Clear;
39   for i := 0 to 10 do
40   begin
41     mmo1.Lines.Add(TMP_Weather[i]);
42   end;  }
43   //TMP_Weather := (htpr1 as WeatherWebServiceSoap).getWeatherbyCityName('59453'); //深圳
44   TMP_Weather := GetWeatherWebServiceSoap(False,'',nil).getWeatherbyCityName('59453');
45   //GetWeatherWebServiceSoap(Self).getWeatherbyCityName('59453');                                                                       //玉林 (59453)
46   mmo1.Lines.Clear;
47   for i := 0 to 22 do
48   begin
49     mmo1.Lines.Add(TMP_Weather[i]);
50   end;
51 end;
52 
53 end.
窗体单元文件
Delphi7 客户端调用WebService(天气预报)第2张Delphi7 客户端调用WebService(天气预报)第7张
 1 object Form1: TForm1
 2   Left = 295
 3   Top = 167
 4   Width = 592
 5   Height = 427
 6   Caption = '天气预报WebService'
 7   Color = clBtnFace
 8   Font.Charset = DEFAULT_CHARSET
 9   Font.Color = clWindowText
10   Font.Height = -11
11   Font.Name = 'MS Sans Serif'
12   Font.Style = []
13   OldCreateOrder = False
14   PixelsPerInch = 96
15   TextHeight = 13
16   object mmo1: TMemo
17     Left = 0
18     Top = 41
19     Width = 576
20     Height = 348
21     Align = alClient
22     Color = clMoneyGreen
23     ImeName = '中文 (简体) - 搜狗拼音输入法'
24     ScrollBars = ssVertical
25     TabOrder = 0
26   end
27   object pnl1: TPanel
28     Left = 0
29     Top = 0
30     Width = 576
31     Height = 41
32     Align = alTop
33     Color = clGradientActiveCaption
34     TabOrder = 1
35     object btn1: TButton
36       Left = 40
37       Top = 8
38       Width = 106
39       Height = 25
40       Caption = '测试天气信息'
41       TabOrder = 0
42       OnClick = btn1Click
43     end
44   end
45 end
窗体文件

免责声明:文章转载自《Delphi7 客户端调用WebService(天气预报)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Hive on Spark 和Hive on mr执行结果不一致原因剖析Maven的默认中央仓库以及修改默认仓库&配置第三方jar包从私服下载下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Linux系统定时任务启动

 分类: linux,shell,python cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业。由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动、关闭这个服务: /sbin/service crond start //启动服务 /sbin/service crond stop //关闭服务 /sbin...

IDEA中导入多个包自动合并为星号

  IDEA中导入同一个包下的几个class会自动合并为星号,如下图。可以通过设置让其不自动合并为星号。 1、选择File→Settings→Editor→Code style→Java,再点击右边的Imports,将Class count to use import with '*'的数量调高即可。 还可以设置一些规则: Names count to...

将xls/csv数据导入到ES中

一.导入MySQL数据库中 (1)通过Navicat导入向导直接导入即可 (2)代码方式导入 【如果字段太多,这种方式做肯定不现实】 csv导入mysql数据库 -- 按需修改即可 package com.blb.mysql_java_es.util; import org.springframework.context.annotation.Bean...

各大巨头电商提供的IP库API接口新浪、搜狐、阿里

新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js     (不可用)新浪多地域测试方法:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=218.192.3.42     (...

linux 之 DolphinScheduler 安装步骤

下载安装包 直接进官网下载 https://dolphinscheduler.apache.org/zh-cn/download/download.html 参考官方文档 https://dolphinscheduler.apache.org/zh-cn/docs/1.3.2/user_doc/cluster-deployment.html 我下载的是1....

教你如何更改android应用的包名

Android 源码自带了很多应用程序,想改个包名方便修改?很简单,两步搞定,以packages/apps/Settings为例: 1、打开AndroidManifest.xml,把 <manifest xmlns:android="http://schemas.android.com/apk/res/android "         androi...