发送 ICQ 通知
原著: Thomas nielsen
问题提出/摘要:
你曾经想过在你的应用程序中包含向一个 ICQ 客户发送一个通知吗?下面就有做法:
回答:
{下面的代码不能解析 wwp.mirabilis.com 的主机名,它直接使用对应的 IP。
(译者注:其实这只要用 HostByName 函数就可以解决。)}
unit unitICQPage;
interface
uses
Windows, Classes, SysUtils, Winsock;
Function ICQPage(dwUIN: DWORD; szFrom, szFromEmail, szSubject, szMsg: String): Integer;
implementation
Function ICQPage(dwUIN: DWORD; szFrom, szFromEmail, szSubject, szMsg: String): Integer;
{This procedure sends an ICQ page.
It uses a raw winsock interface to do this
Return values:
0 = success
1 = WSA Error (WSAGetLastError)
}
Const
ERR_SUCCESS = 0;
ERR_WSA = 1;
Var
MyWSA: WSAData;
SIN: TSockAddr;
hSocket: TSocket;
sRequest: String;
Begin
If WSAStartup(MakeWord(1,1), MyWSA) <> 0 Then Begin
//There's nothing to do, it failed!
ICQPage := ERR_WSA; //ERROR CODE
WSACleanup;
Exit;
end;
hSocket := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
If hSocket = INVALID_SOCKET Then Begin
//Unable to create socket :(
ICQPage := ERR_WSA;
WSACleanup;
Exit;
End;
{Setup "target" information}
SIN.sin_family := AF_INET;
SIN.sin_port := htons(80); //Port 80 is default HTTP (And ofcource the one mirabilis use}
{NOTE: The hostname wwp.mirabilis.com should be resolved in code}
// SIN.sin_addr.S_addr := inet_addr(PChar('127.0.0.1'));
SIN.sin_addr.S_addr := inet_addr(PChar('205.188.252.120'));
If connect(hSocket, SIN, SizeOf(SIN)) = SOCKET_ERROR Then Begin
//Error while connecting...
{Hmm, Now the program should attempt to resolve the hostname wwp.mirabilis.com
and attempt to reconnect to that IP
But since I do not know how to resolve hostnames:}
ICQPage := ERR_WSA;
CloseSocket(hSocket);
WSACleanup;
Exit;
end;
//The socket has connected!
Sleep(30); //dunno, I just feel like it ;-)
sRequest := 'from='+szFrom+'&'+
'fromemail='+szFromEmail+'&'+
'subject='+szSubject+'&'+
'body='+szMsg+'&'+
'to='+IntToStr(dwUIN)+
'&Send=Send+Message';
sRequest := 'POST /scripts/WWPMsg.dll HTTP/1.1'+#13#10+
'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, */*'+#13#10+
'Accept-Language: en'+#13#10+
'Content-Type: application/x-www-form-urlencoded'+#13#10+
'Accept-Encoding: gzip, deflate'+#13#10+
'User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows 98)'+#13#10+
'Host: 127.0.0.1'+#13#10+
'Content-Length: '+IntToStr(Length(sRequest))+#13#10+
'Connection: Keep-Alive'+#13#10+
''+#13#10+
sRequest;
If Send(hSocket, sRequest[1], Length(sRequest), 0) = SOCKET_ERROR Then Begin
//unable to send
ICQPage := ERR_WSA;
CloseSocket(hSocket);
WSACleanup;
Exit;
End;
//Page sent!
CloseSocket(hSocket);
WSACleanup;
ICQPage := ERR_SUCCESS;
end; //ICQPage
end.