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.