Delphi从6.0就开始支持Web Services的开发和应用了,本文通过使用Delphi 7.0调用新浪发送短信的Web Service进行短信应用程序开发这一实例详细的介绍在Delphi中如何开发基于Web Services的应用系统。
第一步,准备工作,了解新浪短信Web Service。新浪发送短信的Web Service地址是http://smsinter.sina.com.cn/ws/smswebservice0101.wsdl,该Web Service就只有一个方法,即string sendXml(carrier,userid,password,mobilenumber,content,msgtype)。各个参数全部为string类型,其含义基本如下(可能不正确)。
Carrier:运营商名称,好像可以随便输,建议输入“Sina”,如果输入其他的值,消息发送的特别慢;
Userid:您在新浪无线上注册的手机ID,如果您没有在http://sms.sina.com.cn上注册您的手机,你是无法使用本Web Service发送短信的;
Password:您在新浪无线上注册手机时所使用的密码;
Mobilenumber:对方的手机号码;
Content:发送短消息的内容;
Msgtype:发送短消息的类型,我估计支持彩信,不过我不知道怎么使用,似乎随便输什么都可以,我使用的是“Text”。
资费标准请参看新浪无线网站上的相关说明,为了不浪费电话费,我没测试使用,应该是一条一角钱,不过也或者是一条两角线,具体不太清楚。由于其后台可能使用了消息队列机制,在繁忙的时候,可能会有较长时间的延迟。
第二步,先建立一个空白的应用程序。运行Delphi 7,打开[File]->[New]->[Application]菜单,Delphi自动生成一个默认的工程。将默认的窗体Form1改为Form_Sms,然后将改工程保存为smsdemo.prj。
第三步,引入Web Service。,打开[File]->[New]->[Other]菜单,在弹出的窗口中选择WebServices Tab页面,然后选择其中的WSDL importer选项,单击OK按钮弹出WSDL importer Wizard窗口,如图2所示。在其上的Location of WSDL File or URL 中输入:http://smsinter.sina.com.cn/ws/smswebservice0101.wsdl (注意,千万不能输错!),单击Next按钮后,再单击Finishi按钮,完成浪发送短信Web Service的引入。此时工程文件中会增加一个名字为smswebservice0101.pas的文件,这是Delphi自动生成的Web Service引入申明文件,不要手工修改他。
第四步,调用Web Service的短信发送接口。在sms窗体中,依次增加四个TLabeledEdit控件,一个TButton控件,2个TMemo控件,
在Form_Sms单元的uses语句中包含smswebservice0101,以便改窗体中能够访问新浪发送短信的Web Service接口。
在发送按钮onClick事件中写入以下代码:
procedure Tsms.Button1Click(Sender: TObject);
begin
Memo_result.Text := GetSMSWebServiceSoapPort.sendXml(''sina'',
LabeledEdit_UserID.Text,LabeledEdit_Pass.Text,
LabeledEdit_PhoneNumber.Text, Memo_Msg.Text, ''Text'');
end;
注意:参数分别对应运营商、用户名、密码、对方号码、消息内容;Memo1对应界面上的反馈。
第五步,使用Web Service的短信发送接口进行短信发送。编译、运行smsdemo.prj,在用户名中输入您在新浪无线上注册的手机ID、密码中输入您在新浪无线上的密码,然后再输入对方的手机号码和消息内容,单击发送按钮,稍候片刻,在反馈信息框中将会得到该Web Service的反馈信息,如果一切正常,系统会提示您短信发送成功。
附*.PAS和*.DFM源码如下:
-------------Unit_Sms.pas 文件----------
unit Unit_Sms;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm_Sms = class(TForm)
Button1: TButton;
Memo_Msg: TMemo;
LabeledEdit_UserID: TLabeledEdit;
LabeledEdit_Pass: TLabeledEdit;
LabeledEdit_PhoneNumber: TLabeledEdit;
LabeledEdit_MsgType: TLabeledEdit;
LabeledEdit_Carrier: TLabeledEdit;
Memo_Result: TMemo;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form_Sms: TForm_Sms;
implementation
{$R *.dfm}
USES smswebservice0101;
procedure TForm_Sms.Button1Click(Sender: TObject);
begin
Button1.Enabled := False;
Memo_result.Text := GetSMSWebServiceSoapPort.sendXml(LabeledEdit_Carrier.Text,
LabeledEdit_UserID.Text,LabeledEdit_Pass.Text, LabeledEdit_PhoneNumber.Text,
Memo_Msg.Text, LabeledEdit_MsgType.Text);
Button1.Enabled := True;
end;
end.
--------------Unit_Sms.dfm文件------------------
object Form_Sms: TForm_Sms
Left = 317
Top = 184
BorderIcons = [biSystemMenu]
BorderStyle = bsSingle
Caption = '调用C# WEB SERVICE'
ClientHeight = 434
ClientWidth = 640
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -16
Font.Name = 'MS Sans Serif'
Font.Style = []
KeyPreview = True
OldCreateOrder = False
Position = poDefault
Visible = True
PixelsPerInch = 96
TextHeight = 20
object Label1: TLabel
Left = 16
Top = 80
Width = 96
Height = 20
Caption = '短信信息: '
end
object Memo_Msg: TMemo
Left = 8
Top = 112
Width = 609
Height = 169
Lines.Strings = (
'')
TabOrder = 3
end
object LabeledEdit_UserID: TLabeledEdit
Left = 8
Top = 40
Width = 225
Height = 28
EditLabel.Width = 241
EditLabel.Height = 20
EditLabel.Caption = '新浪无线上注册的手机ID: '
TabOrder = 0
Text = 'sendmsgtest'
end
object LabeledEdit_Pass: TLabeledEdit
Left = 256
Top = 40
Width = 185
Height = 28
EditLabel.Width = 96
EditLabel.Height = 20
EditLabel.Caption = '密码: '
TabOrder = 1
Text = 'sendmsgtest'
end
object LabeledEdit_PhoneNumber: TLabeledEdit
Left = 456
Top = 40
Width = 161
Height = 28
EditLabel.Width = 172
EditLabel.Height = 20
EditLabel.Caption = '接收短信手机号: '
TabOrder = 2
Text = '13876010758'
end
object LabeledEdit_MsgType: TLabeledEdit
Left = 112
Top = 312
Width = 89
Height = 28
EditLabel.Width = 84
EditLabel.Height = 20
EditLabel.Caption = '消息类型: '
TabOrder = 7
Text = 'Text'
Visible = False
end
object LabeledEdit_Carrier: TLabeledEdit
Left = 16
Top = 312
Width = 73
Height = 28
EditLabel.Width = 68
EditLabel.Height = 20
EditLabel.Caption = '运营商: '
TabOrder = 6
Text = 'sina'
Visible = False
end
object Memo_Result: TMemo
Left = 224
Top = 304
Width = 385
Height = 41
TabOrder = 5
end
object Button1: TButton
Left = 480
Top = 360
Width = 97
Height = 41
Caption = '发送 '
TabOrder = 4
OnClick = Button1Click
end
end
---------------引入Web Service后,DELPHI自动产生的smswebservice0101.pas文件------------
// ************************************************************************ //
// The types declared in this file were generated from data read from the
// WSDL File described below:
// WSDL : http://smsinter.sina.com.cn/ws/smswebservice0101.wsdl
// Encoding : UTF-8
// Version : 1.0
// (2009-10-22 09:19:26 - 1.33.2.5)
// ************************************************************************ //
unit smswebservice0101;
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"
// ************************************************************************ //
// Namespace : http://outlook.microsoft.com/add-ins/SMS/message/
// soapAction: http://outlook.microsoft.com/add-ins/SMS/action/SMSWebService.sendXml
// transport : http://schemas.xmlsoap.org/soap/http
// style : rpc
// binding : SMSWebServiceSoapBinding
// service : SMSWS
// port : SMSWebServiceSoapPort
// URL : http://202.108.35.168/cgi-bin/ws/smswebservice0101
// ************************************************************************ //
SMSWebServiceSoapPort = interface(IInvokable)
['{640A28C1-F5F5-8CD9-06A9-58576FCA9BBA}']
function sendXml(const Carrier: WideString; const Id: WideString; const Password: WideString; const ToMobile: WideString; const Message: WideString; const MsgType: WideString): WideString; stdcall;
end;
function GetSMSWebServiceSoapPort(UseWSDL: Boolean=System.False; Addr: string=''; HTTPRIO: THTTPRIO = nil): SMSWebServiceSoapPort;
implementation
function GetSMSWebServiceSoapPort(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): SMSWebServiceSoapPort;
const
defWSDL = 'http://smsinter.sina.com.cn/ws/smswebservice0101.wsdl';
defURL = 'http://202.108.35.168/cgi-bin/ws/smswebservice0101';
defSvc = 'SMSWS';
defPrt = 'SMSWebServiceSoapPort';
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 SMSWebServiceSoapPort);
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(SMSWebServiceSoapPort), 'http://outlook.microsoft.com/add-ins/SMS/message/', 'UTF-8');
InvRegistry.RegisterDefaultSOAPAction(TypeInfo(SMSWebServiceSoapPort), 'http://outlook.microsoft.com/add-ins/SMS/action/SMSWebService.sendXml');
end.