Jeg benytter Delphi 7, Indy 10.2.3 (TIdHTTP komponenten) og BiBoB´s SOAP funktioner som ses her:
https://www.bibob.dk/SmsSender.asmxJeg lavede det ret hurtig og kendte intet til SOAP og havde ikke lige tid til at sætte mig ind i det så det kan helt sikkert laves meget bedre.
Først nogle types og consts:
type
TSOAP_Message_Type = (SOAP_Login, SOAP_GetBalance, SOAP_GetNumbers, SOAP_GetPricePlan, SOAP_GetRemoteContacts, SOAP_SendMessage, SOAP_ScheduleMessage);
.
.
SOAP_Std_CellPass = '<cellphone></cellphone><password></password>';
SOAP_Login_XML = '<Login xmlns="
http://www.bibob.dk/">'+SOAP_Std_CellPass+'</Login>'; SOAP_GetBalance_XML = '<GetBalance xmlns="
http://www.bibob.dk/">'+SOAP_Std_CellPass+'</GetBalance>'; SOAP_GetNumbers_XML = '<GetNumbers xmlns="
http://www.bibob.dk/">'+SOAP_Std_CellPass+'</GetNumbers>'; SOAP_GetPricePlan_XML = '<GetPricePlan xmlns="
http://www.bibob.dk/">'+SOAP_Std_CellPass+'</GetPricePlan>'; SOAP_GetRemoteContacts_XML = '<GetRemoteContacts xmlns="
http://www.bibob.dk/">'+SOAP_Std_CellPass+'</GetRemoteContacts>'; SOAP_SendMessage_XML = '<SendMessage xmlns="
http://www.bibob.dk/">'+SOAP_Std_CellPass+'<smsTo></smsTo><smscontents>string</smscontents><fromNumber></fromNumber></SendMessage>'; SOAP_ScheduleMessage_XML = '<ScheduleMessage xmlns="
http://www.bibob.dk/">'+SOAP_Std_CellPass+'<smsTo></smsTo><smscontents></smscontents><fromNumber></fromNumber><sendDate></sendDate></ScheduleMessage>';.
.
var
SOAP_Send_XML : string;
.
.
Selve kommunukationen med bibob har jeg lavet i en tråd:
TSOAPThread = class(TThread)
private
{ Private declarations }
pResultElement, pBody : string;
Result, FullResult : string;
IdHTTP: TIdHTTP;
IdSSLIOHandlerSocketOpenSSL: TIdSSLIOHandlerSocketOpenSSL;
procedure SetResult;
protected
procedure Execute; override;
public
SOAP_Message_Type : TSOAP_Message_Type;
constructor Create(Body, ResultElement : string; SOAP_Message_Type : TSOAP_Message_Type);
// destructor Destroy; override;
end;
// ****************************** TSOAPThread START ****************************************************************
constructor TSOAPThread.Create(Body, ResultElement : string; SOAP_Message_Type : TSOAP_Message_Type);
begin
inherited Create(False);
FreeOnTerminate:=true;
pResultElement:=ResultElement;
pBody:=Body;
Self.SOAP_Message_Type:=SOAP_Message_Type;
end;
procedure TSOAPThread.SetResult;
var
i,b : integer;
begin
b:=Length(pResultElement);
i:=Pos(pResultElement,Result);
if i=0 then
begin
pResultElement:=Copy(pResultElement,1,b-1)+' />'; // f.eks. <LoginResult />
i:=Pos(pResultElement,Result);
end;
if i=0 then
Result:='ERR2'
else
begin
Insert('/',pResultElement,2); //</LoginResult>
Result:=Copy(Result,i+b,Pos(pResultElement,Result)-i-b);
end;
// dette returneres hvis rssultat er tomt
//<LoginResult /></LoginResponse><
end;
procedure TSOAPThread.Execute;
var
aStream: TMemoryStream;
Params: TMemoryStream;
const
SOAP_Top_XML = '<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="
http://www.w3.org/2001/XMLSchema" xmlns:soap12="
http://www.w3.org/2003/05/soap-envelope"><soap12:Body>'; SOAP_Bottom_XML = '</soap12:Body></soap12:Envelope>';
begin
aStream := TMemoryStream.create;
Params := TMemoryStream.Create;
IdHTTP:=TIdHTTP.Create;
IdSSLIOHandlerSocketOpenSSL:=TIdSSLIOHandlerSocketOpenSSL.Create;
IdSSLIOHandlerSocketOpenSSL.SSLOptions.Method:=sslvSSLv23;
IdHTTP.IOHandler:=IdSSLIOHandlerSocketOpenSSL;
IdHTTP.HTTPOptions:=[hoKeepOrigProtocol,hoForceEncodeParams];
IdHTTP.Request.ContentType:='application/soap+xml; charset=utf-8';
IdHTTP.Request.Host:='
www.bibob.dk'; pBody:=SOAP_Top_XML+pBody+SOAP_Bottom_XML;
IdHTTP.Request.ContentLength:=Length(pBody);
Params.Write(pBody[1],length(pBody));
Result:='';
try
IdHTTP.ReadTimeout:=5000;
IdHTTP.Post('
https://www.bibob.dk/SmsSender.asmx', Params,aStream);
aStream.Position:=0;
SetLength(Result, aStream.Size);
aStream.ReadBuffer(Pointer(Result)^, aStream.Size);
FullResult:=Result;
except
Result:='ERR';
end;
aStream.Free;
Params.Free;
IdSSLIOHandlerSocketOpenSSL.Free;
IdHTTP.Free;
if Result<>'ERR' then
SetResult;
end;
// ****************************** TSOAPThread SLUT ****************************************************************
Trådens OnTerminate procedure..
procedure TMainForm.SOAPOnTerminate(Sender: TObject);
var
Result, FullResult : string;
SOAP_Message_Type : TSOAP_Message_Type;
ResultCode : integer;
ErrorString : string;
begin
Result:=(Sender as TSOAPThread).Result;
FullResult:=(Sender as TSOAPThread).FullResult;
ResultCode:=StrToIntDef(GetXMLBlok(Result,'<ResultCode>'),0);
ErrorString:=GetXMLBlok(Result,'<ErrorString>');
// <ResultCode>0</ResultCode><ErrorString>Ingen fejl.</ErrorString>
SkrivDebugLog(FullResult,false);
SOAP_Message_Type:=(Sender as TSOAPThread).SOAP_Message_Type;
if (Result='ERR') or (result='ERR2') then
begin
SetStatusText('FEJL: '+Result,1,0);
exit;
end;
if SOAP_Message_Type=SOAP_Login then
begin
if Result<>'' then
begin
LoginStatus:=0;
SetStatusText('FEJL ved check af mobilnummer og adgangskode: '+Result,1,0);
end
else
begin
LoginStatus:=2;
SetStatusText('Mobilnummer og adgangskode er i orden',2,5);
CheckSaldo;
GetNumbers;
GetPricePlan;
end;
end;
osv. med resten af SOAP_Message_Type´erne
.
.
Min "Logon" procedure ser således ud:
procedure TMainForm.Logon;
begin
if not IsMobilnummerOgAdgangskodeValid then
exit;
SOAP_Send_XML:=SOAP_Login_XML;
SetXML_Nummer_Kode;
LoginStatus:=1;
SetStatusText('Tester mobilnummer og adgangskode',1,0);
OpretSOAPThread(SOAP_Send_XML, '<LoginResult>', SOAP_Login);
end;
.
.
Og så de funktioner der bruges:
procedure TMainForm.SetXML_Nummer_Kode;
var
Mobilnummer : string;
Adgangskode : string;
begin
GetMobilNummerOgAdgangskode(Mobilnummer,Adgangskode);
SetXMLValue('<cellphone>',SOAP_Send_XML,Mobilnummer);
SetXMLValue('<password>',SOAP_Send_XML,Adgangskode);
end;
Jeg har mobil nummer og koden (MD5) gemt i en ini fil:
procedure TMainForm.GetMobilnummerOgAdgangskode(var Mobilnummer, Adgangskode : string);
begin
Mobilnummer:=SystemIni.ReadString(SystemIni_Sec,EditMobilnummer.Hint,'');
Adgangskode:=SystemIni.ReadString(SystemIni_Sec,EditAdgangskode.Hint,'');
end;
Proceduren der indsætter værdier:
procedure TMainForm.SetXMLValue(Element : string; var XML : string; Value : string);
var
i,x : integer;
EndTag : string;
begin
i:=Pos(Element,XML);
if i=0 then
exit;
EndTag:=Element;
Insert('/',EndTag,2);
x:=Pos(EndTag,XML);
if x=0 then
exit;
i:=i+Length(Element);
Delete(XML,i,x-i); // fjerner en evt. tekst der allerede står der
Insert(Value, XML, i);
end;
Og så proceduren der opretter "SOAP" tråden:
procedure TMainForm.OpretSOAPThread(Body, ResultElement: string;
SOAP_Message_Type: TSOAP_Message_Type);
begin
SkrivDebugLog(Body,true);
TSOAPThread.Create(Body, ResultElement, SOAP_Message_Type).OnTerminate:=SOAPOnTerminate;
end;