1 {*******************************************************}
3 { DOLIB API Interface Unit }
5 { 2002 Monazilla Project }
6 { Dax mailto:daxmonazilla@yahoo.co.jp }
7 {
\81i
\83q
\81jmailto:gikonavi@ice.dti2.ne.jp }
8 {********************************************************
12 2002/03/02
\83\8d\83O
\83C
\83\93\83G
\83\89\81[
\82ð
\8c\9f\8fo
\82·
\82é
\82æ
\82¤
\82É
\8fC
\90³
\82µ
\82½
\82©
\82à
\81B
13 2002/03/02 DOLIB.dll
\82ð
\8eg
\82í
\82È
\82¢
\82æ
\82¤
\82É
\82µ
\82½
\81B
14 2002/02/27
\83o
\83O
\8fC
\90³ (GetVersion
\82Í
\83R
\83l
\83N
\83g
\82µ
\82Ä
\82È
\82
\82Ä
\82à
\8eæ
\93¾
\89Â
\94\
\82É
\82µ
\82½)
15 2002/01/22 DOLIB 1.00C
\91Î
\89\9e\81B
16 \88È
\89º
\82Ì
\83v
\83\8d\83p
\83e
\83B
\82ð
\92Ç
\89Á
\81B
17 - Session .........
\83Z
\83b
\83V
\83\87\83\93\82Ì
\83|
\83C
\83\93\83^
\82ð
\95Ô
\82µ
\82Ü
\82·
\81A
\91½
\95ª
\8eg
\82í
\82È
\82¢
\81B
18 - SessionID .......
\83Z
\83b
\83V
\83\87\83\93ID
\82ð
\95Ô
\82µ
\82Ü
\82·
\81B
19 - Version ......... DOLIB
\82Ì
\83o
\81[
\83W
\83\87\83\93\82ð
\95Ô
\82µ
\82Ü
\82·
\81B
20 - UserAgent ....... UA
\97p
\82Ì
\95¶
\8e\9a\97ñ Monazilla/x.xx
\82ð
\95Ô
\82µ
\82Ü
\82·
\81B
21 - ErrorCode .......
\83G
\83\89\81[
\83R
\81[
\83h
\82ð
\95Ô
\82µ
\82Ü
\82·
\81B
22 - ErrorMsg ........
\83G
\83\89\81[
\83\81\83b
\83Z
\81[
\83W
\82ð
\95Ô
\82µ
\82Ü
\82·
\81B
23 2002/01/20 Disconnect
\8cã
\82É Connected
\83v
\83\8d\83p
\83e
\83B
\82ð
\96ß
\82µ
\82Ä
\82È
\82©
\82Á
\82½
\81B
24 2002/01/19 DOLIB 1.00B
\91Î
\89\9e\81B
\83f
\81[
\83^
\8eæ
\93¾
\82É
\90¬
\8c÷
\81I
25 2002/01/18 DOLIB 1.00
\91Î
\89\9e\81B
\82µ
\82©
\82µ
\83G
\83\89\81[
\82µ
\82©
\95Ô
\82Á
\82Ä
\97\88\82È
\82¢
\81A
\81A
26 2002/01/18 ghanyan
\8e\81\82Ì
\8f\95\8c¾
\82É
\82æ
\82è
\93®
\8dì
\82·
\82é
\81B
\8a´
\8eÓ
\81I
27 2002/01/09 DOLIB 0.01
\97p
\82É
\8dì
\90¬
\8aJ
\8en
\81B
\82Å
\82à
\93®
\82©
\82È
\82¢
\82Ì
\82Å
\82Ù
\82Á
\82Æ
\82
\81B
36 Windows, SysUtils, WinInet;
39 TDolibSession = class(TObject)
46 property SessionID: string read FSessionID write FSessionID;
47 property ErrorCode: Integer read FErrorCode write FErrorCode;
48 property ErrorString: string read FErrorString write FErrorString;
49 property UserAgent: string read FUserAgent write FUserAgent;
52 TDolib = class(TObject)
54 FSession : TDolibSession;
59 FProxyAddress: string;
62 function GetSessionID: string;
63 function GetVersion: string;
64 function GetUserAgent: string;
65 function GetErrorCode: integer;
66 function GetErrorMsg: string;
67 procedure MakeError(Session: TDolibSession; Error: DWORD);
68 procedure DOLIB_LOGIN(Proxy: string; Port: Integer; ID: string; Pass: string);
69 procedure ForcedDOLIB_LOGIN(Proxy: string; Port: Integer; ID: string; Pass: string);
72 destructor Destroy; override;
73 function Connect: boolean;
74 function ForcedConnect: boolean; //SSL
\8fá
\8aQ
\97p
\8b
\90§
\83\8d\83O
\83C
\83\93
75 function Disconnect: boolean;
76 property ProxyAddress: string read FProxyAddress write FProxyAddress;
77 property ProxyPort: integer read FProxyPort write FProxyPort;
78 property UserName: string read FUserName write FUserName;
79 property Password: string read FPassword write FPassword;
80 property ClientUA: string read FClientUA write FClientUA;
81 property Connected: boolean read FConnected;
82 property SessionID: string read GetSessionID;
83 property Version: string read GetVersion;
84 property UserAgent: string read GetUserAgent;
85 property ErrorCode: integer read GetErrorCode;
86 property ErrorMsg: string read GetErrorMsg;
87 property DolibURL: string read FDolibURL write FDolibURL;
92 DOLIB_VERSION = $10000;
93 DOLIB_LOGIN_UA = 'DOLIB/1.00';
94 DOLIB_LOGIN_HOST = 'tiger2.he.net';
95 // DOLIB_LOGIN_URL = '/~tora3n2c/futen.cgi';
96 DOLIB_2CH_UA = 'X-2ch-UA:';
97 // DOLIB_2CH_UA = 'X-2ch-UA: gikoNavi/1.00'#13#10;
98 DOLIB_ENOMEM_STRING = '
\83\81\83\82\83\8a\82ª
\91«
\82è
\82Ü
\82¹
\82ñ
\81B';
99 DOLIB_LOGIN_ERROR = 'ERROR:';
103 constructor TDolib.Create;
109 destructor TDolib.Destroy;
116 function TDolib.Connect: boolean;
119 if not Connected then begin
120 DOLIB_LOGIN(FProxyAddress, FProxyPort, FUserName, FPassword);
122 if (AnsiPos(DOLIB_LOGIN_ERROR, SessionID) = 1) then begin
125 end else if ErrorCode <> 0 then begin
130 // Result := (ErrorCode = 0);
134 function TDolib.ForcedConnect: boolean; //2003/12/20
\82Ü
\82Å
\82ÌSSL
\8fá
\8aQ
\97p
\8b
\90§
\83\8d\83O
\83C
\83\93\81i12/21
\88È
\8d~
\82È
\82ç
\92Ê
\8fí
\83\8d\83O
\83C
\83\93\81j
137 if not Connected then begin
138 ForcedDOLIB_LOGIN(FProxyAddress, FProxyPort, FUserName, FPassword);
143 function TDolib.Disconnect: boolean;
146 if FSession <> nil then
147 FreeAndNil(FSession);
151 function TDolib.GetVersion: string;
159 Result := Format('%d.%.2d', [mj, mn]);
162 function TDolib.GetSessionID: string;
165 Result := FSession.FSessionID
170 function TDolib.GetUserAgent: string;
173 Result := FSession.FUserAgent
178 function TDolib.GetErrorMsg: string;
181 Result := FSession.FErrorString
183 Result := 'Error: ID
\82©
\83p
\83X
\83\8f\81[
\83h
\82ª
\90³
\82µ
\82
\82 \82è
\82Ü
\82¹
\82ñ
\81B';
186 function TDolib.GetErrorCode: integer;
189 Result := FSession.ErrorCode
194 procedure TDolib.MakeError(Session: TDolibSession; Error: DWORD);
196 Buf: array[0..4096] of Char;
198 Session.ErrorCode := Error;
199 if Error = ERROR_NOT_ENOUGH_MEMORY then
200 Session.ErrorString := DOLIB_ENOMEM_STRING
202 FillChar(Buf, SizeOf(Buf), #0);
203 FormatMessage({FORMAT_MESSAGE_ALLOCATE_BUFFER or}
204 FORMAT_MESSAGE_IGNORE_INSERTS or
205 FORMAT_MESSAGE_FROM_SYSTEM or
206 FORMAT_MESSAGE_FROM_HMODULE,
207 Pointer(GetModuleHandle('wininet')), Error,
208 (((Word(SUBLANG_DEFAULT)) shl 10) or Word(LANG_NEUTRAL)), //Delphi
\82ÉMAKELANGID
\83}
\83N
\83\8d\82ª
\96³
\82©
\82Á
\82½
\82Ì
\81B(
\81L¥
\83Ö¥`)¼®ÎÞ°Ý
209 // MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
210 Buf, SizeOf(Buf), nil);
211 Session.ErrorString := Buf;
216 kage
\8dì
\8eÒ
\82³
\82ñ
\82ÌDOLIB
\83N
\83\8d\81[
\83\93\83\
\81[
\83X
\81i
\91å
\95Ï
\82¨
\82¢
\82µ
\82ã
\82¤
\82²
\82´
\82¢
\82Ü
\82µ
\82½
\81j
217 http://members.jcom.home.ne.jp/monazilla/document/wininetdel.html
218 http://support.microsoft.com/default.aspx?scid=kb;EN-US;q168151
219 http://msdn.microsoft.com/library/default.asp?url=/workshop/networking/wininet/wininet.asp
220 http://homepage1.nifty.com/~suzuki/delphi/wininet.html
222 procedure TDolib.DOLIB_LOGIN(Proxy: string; Port: Integer; ID: string; Pass: string);
227 ProxyHostPort: string;
228 Buf: array[0..4096] of Char;
234 FSession := TDolibSession.Create;
236 if Proxy <> '' then begin
237 ProxyHostPort := Format('%s:%d', [Proxy, Port]);
238 hSession := InternetOpen(DOLIB_LOGIN_UA, INTERNET_OPEN_TYPE_PROXY, PChar(ProxyHostPort), '', 0);
240 hSession := InternetOpen(DOLIB_LOGIN_UA, INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);
243 if not Assigned(hSession) then
244 MakeError(FSession, GetLastError())
246 hConnect := InternetConnect(hSession, DOLIB_LOGIN_HOST,
247 INTERNET_DEFAULT_HTTPS_PORT, nil, nil,
248 INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
249 if not Assigned(hConnect) then
250 MakeError(FSession, GetLastError())
252 hRequest := HttpOpenRequest(hConnect, 'POST', PChar(FDolibURL),
254 INTERNET_FLAG_NO_CACHE_WRITE or INTERNET_FLAG_NO_COOKIES or
255 INTERNET_FLAG_NO_UI or INTERNET_FLAG_SECURE, 0);
256 if not Assigned(hRequest) then
257 MakeError(FSession, GetLastError())
259 UserInfo := Format('ID=%s&PW=%s', [ID, Pass]);
260 UserAgent := Format('%s %s', [DOLIB_2CH_UA, ClientUA]) + #13#10;
261 if not HttpSendRequest(hRequest, PChar(UserAgent), DWORD(-1), PChar(UserInfo), Length(UserInfo)) then
262 MakeError(FSession, GetLastError())
264 if not InternetReadFile(hRequest, @Buf, SizeOf(Buf), cb) then
265 MakeError(FSession, GetLastError())
266 else if (cb < 11) or (Pos('SESSION-ID=', Buf) <> 1) then
267 MakeError(FSession, ERROR_INVALID_DATA)
269 if Buf[cb - 1] = #10 then
271 FSession.SessionID := Copy(Buf, 12, cb);
272 if FSession.SessionID = '' then
273 MakeError(FSession, ERROR_NOT_ENOUGH_MEMORY);
274 Delim := Pos(':', Buf);
276 MakeError(FSession, ERROR_INVALID_DATA)
278 FSession.UserAgent := Copy(Buf, 12, Delim - 12);
279 if FSession.UserAgent = '' then
280 MakeError(FSession, ERROR_NOT_ENOUGH_MEMORY);
284 InternetCloseHandle(hRequest);
286 InternetCloseHandle(hConnect);
288 InternetCloseHandle(hSession);
291 //SSL
\8fá
\8aQ
\97p
\8b
\90§
\83\8d\83O
\83C
\83\93
292 procedure TDolib.ForcedDOLIB_LOGIN(Proxy: string; Port: Integer; ID: string; Pass: string);
297 ProxyHostPort: string;
298 Buf: array[0..4096] of Char;
304 FSession := TDolibSession.Create;
306 if Proxy <> '' then begin
307 ProxyHostPort := Format('%s:%d', [Proxy, Port]);
308 hSession := InternetOpen(DOLIB_LOGIN_UA, INTERNET_OPEN_TYPE_PROXY, PChar(ProxyHostPort), '', 0);
310 hSession := InternetOpen(DOLIB_LOGIN_UA, INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);
313 if not Assigned(hSession) then
314 MakeError(FSession, GetLastError())
316 hConnect := InternetConnect(hSession, DOLIB_LOGIN_HOST,
317 INTERNET_DEFAULT_HTTPS_PORT, nil, nil,
318 INTERNET_SERVICE_HTTP, INTERNET_FLAG_SECURE, 0);
319 if not Assigned(hConnect) then
320 MakeError(FSession, GetLastError())
322 hRequest := HttpOpenRequest(hConnect, 'POST', PChar(FDolibURL),
324 INTERNET_FLAG_NO_CACHE_WRITE or INTERNET_FLAG_NO_COOKIES or
325 INTERNET_FLAG_NO_UI or INTERNET_FLAG_SECURE, 0);
326 if not Assigned(hRequest) then
327 MakeError(FSession, GetLastError())
329 UserInfo := Format('ID=%s&PW=%s', [ID, Pass]);
330 UserAgent := Format('%s %s', [DOLIB_2CH_UA, ClientUA]) + #13#10;
331 HttpSendRequest(hRequest, PChar(UserAgent), DWORD(-1), PChar(UserInfo), Length(UserInfo));
332 if not InternetReadFile(hRequest, @Buf, SizeOf(Buf), cb) then
333 MakeError(FSession, GetLastError())
334 else if (cb < 11) or (Pos('SESSION-ID=', Buf) <> 1) then
335 MakeError(FSession, ERROR_INVALID_DATA)
337 if Buf[cb - 1] = #10 then
339 FSession.SessionID := Copy(Buf, 12, cb);
340 if FSession.SessionID = '' then
341 MakeError(FSession, ERROR_NOT_ENOUGH_MEMORY);
342 Delim := Pos(':', Buf);
344 MakeError(FSession, ERROR_INVALID_DATA)
346 FSession.UserAgent := Copy(Buf, 12, Delim - 12);
347 if FSession.UserAgent = '' then
348 MakeError(FSession, ERROR_NOT_ENOUGH_MEMORY);
351 InternetCloseHandle(hRequest);
353 InternetCloseHandle(hConnect);
355 InternetCloseHandle(hSession);