TNB Library
TnbInternetHandle.h
[詳解]
1#pragma once
13#include "TnbDynamicFunc.h"
14#include "TnbPointerHandle.h"
15#include "TnbStrVector.h"
16
17
18
19#include <Wininet.h>
20#pragma comment(lib,"Wininet.lib")
21
22
23
24//TNB Library
25namespace TNB
26{
27
28
29
30#ifndef _TnbDOXYGEN //Document作成用シンボル
32 struct TPhInternetCloseHandle
33 {
34 void operator()(HINTERNET P) { ::InternetCloseHandle(P); }
35 };
36#endif
37
38
39
51
52
53
66{
67 CInternetHandleHandle m_handle;
68
69public:
70
75 CInternetHandle(HINTERNET h = NULL) : m_handle(h)
76 {
77 }
78
83 CInternetHandle(const CInternetHandle& other) : m_handle(other.m_handle)
84 {
85 }
86
93 {
94 m_handle = other.m_handle;
95 return *this;
96 }
97
103 void Close(void)
104 {
105 m_handle.Null();
106 }
107
113 bool IsValid(void) const
114 {
115 return ! m_handle.IsNull();
116 }
117
134 bool Open(LPCTSTR lpszAgent, DWORD dwAccessType, LPCTSTR lpszProxy = NULL,
135 LPCTSTR lpszProxyBypass = NULL, DWORD dwFlags = 0)
136 {
137 m_handle = ::InternetOpen(lpszAgent, dwAccessType, lpszProxy, lpszProxyBypass, dwFlags);
138 return ! m_handle.IsNull();
139 }
140
162 CInternetHandle Connect(LPCTSTR lpszServerName, INTERNET_PORT tPort, LPCTSTR lpszUserName,
163 LPCTSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext = 0)
164 {
165 return ::InternetConnect(m_handle, lpszServerName, tPort, lpszUserName, lpszPassword, dwService, dwFlags, dwContext);
166 }
167
184 CInternetHandle OpenUrl(LPCTSTR lpszUrl, LPCTSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext = 0)
185 {
186 return ::InternetOpenUrl(m_handle, lpszUrl, lpszHeaders, dwHeadersLength, dwFlags, dwContext);
187 }
188
204 CInternetHandle HttpOpenRequest(LPCTSTR lpszVerb, LPCTSTR lpszObjectName, LPCTSTR lpszVersion, LPCTSTR lpszReferrer, DWORD dwFlags, DWORD_PTR dwContext = 0)
205 {
206 return ::HttpOpenRequest(m_handle, lpszVerb, lpszObjectName, lpszVersion, lpszReferrer, NULL, dwFlags, dwContext);
207
208 }
209
226 CInternetHandle HttpOpenRequest(LPCTSTR lpszVerb, LPCTSTR lpszObjectName, LPCTSTR lpszVersion, LPCTSTR lpszReferrer, const CStrVector& acceptTypes, DWORD dwFlags, DWORD_PTR dwContext = 0)
227 {
228 CWorkMemT<LPCTSTR> a(acceptTypes.GetSize() + 1);
229 loop ( i, acceptTypes )
230 {
231 a[i] = acceptTypes.ReferBuffer()->ReferBuffer();
232 }
233 a[acceptTypes.GetSize()] = NULL;
234 return ::HttpOpenRequest(m_handle, lpszVerb, lpszObjectName, lpszVersion, lpszReferrer, a, dwFlags, dwContext);
235 }
236
246 bool HttpSendRequest(LPCTSTR lpszHeaders, DWORD dwHeadersLength, LPVOID lpOptional, DWORD dwOptionalLength)
247 {
248 return !! ::HttpSendRequest(m_handle, lpszHeaders, dwHeadersLength, lpOptional, dwOptionalLength);
249 }
250
260 bool HttpQueryInfo(DWORD dwInfoLevel, LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
261 {
262 return !! ::HttpQueryInfo(m_handle, dwInfoLevel, lpBuffer, lpdwBufferLength, lpdwIndex);
263 }
264
304 bool QueryOption(DWORD dwOption, LPVOID _lpBuffer, LPDWORD _lpdwBufferLength) const
305 {
306 return !! ::InternetQueryOption(m_handle, dwOption, _lpBuffer, _lpdwBufferLength);
307 }
308
320 bool ReadFile(LPVOID _lpBuffer, DWORD dwNumberOfBytesToRead, LPDWORD _lpdwNumberOfBytesRead) const
321 {
322 return !! ::InternetReadFile(m_handle, _lpBuffer, dwNumberOfBytesToRead, _lpdwNumberOfBytesRead);
323 }
324
340 DWORD SetFilePointer(LONG lDistanceToMove, LONG* pReserved, DWORD dwMoveMethod, DWORD_PTR dwContext = 0) const
341 {
342 return ::InternetSetFilePointer(m_handle, lDistanceToMove, pReserved, dwMoveMethod, dwContext);
343 }
344
371 bool SetOption(DWORD dwOption, LPVOID lpBuffer, DWORD dwBufferLength)
372 {
373 return !! ::InternetSetOption(m_handle, dwOption, lpBuffer, dwBufferLength);
374 }
375
385 INTERNET_STATUS_CALLBACK SetStatusCallback(INTERNET_STATUS_CALLBACK pInetProc)
386 {
387 return ::InternetSetStatusCallback(m_handle, pInetProc);
388 }
389
399 bool FindNextFile(LPVOID pFindData) const
400 {
401 return !! ::InternetFindNextFile(m_handle, pFindData);
402 }
403
414 bool WriteFile(LPCVOID lpBuffer, DWORD dwNumberOfBytesToWrite, LPDWORD lpdwNumberOfBytesWritten)
415 {
416 return !! ::InternetWriteFile(m_handle, lpBuffer, dwNumberOfBytesToWrite, lpdwNumberOfBytesWritten);
417 }
418
427 bool CreateDirectory(LPCTSTR lpszDirName)
428 {
429 return !! ::FtpCreateDirectory(m_handle, lpszDirName);
430 }
431
440 bool DeleteFile(LPCTSTR lpszFileName)
441 {
442 return !! ::FtpDeleteFile(m_handle, lpszFileName);
443 }
444
456 CInternetHandle FindFirstFile(LPCTSTR lpszSearchFile, LPWIN32_FIND_DATA lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext = 0) const
457 {
458 return ::FtpFindFirstFile(m_handle, lpszSearchFile, lpFindFileData, dwFlags, dwContext);
459 }
460
470 bool GetCurrentDirectory(LPTSTR _lpszCurrentDirectory, LPDWORD _lpdwLength) const
471 {
472 return !! ::FtpGetCurrentDirectory(m_handle, _lpszCurrentDirectory, _lpdwLength);
473 }
474
504 bool GetFile(LPCTSTR lpszRemoteFile, LPCTSTR lpszLocalFile, BOOL fFailIfExists,
505 DWORD dwFlagsAndAttributes, DWORD dwFlags, DWORD_PTR dwContext = 0) const
506 {
507 return !! ::FtpGetFile(m_handle, lpszRemoteFile, lpszLocalFile, fFailIfExists, dwFlagsAndAttributes, dwFlags, dwContext);
508 }
509
517 DWORD GetFileSize(LPDWORD _lpdwFileSizeHigh) const
518 {
519 typedef DWORD (WINAPI* P)(HINTERNET, LPDWORD);
520 try
521 {
522 return CDynamicFuncT<P>("Wininet.dll", "FtpGetFileSize")()(m_handle, _lpdwFileSizeHigh);
523 }
524 catch(CNullPointerException& e)
525 {
526 ASSERT0(false, "CInternetHandle::GetFileSize()",
527 "API FtpGetFileSize()が見つかりません。\nWininet.dll が古い可能性があります。");
528 e.OnCatch();
529 }
530 return 0;
531 }
532
556 CInternetHandle OpenFile(LPCTSTR lpszFileName, DWORD dwAccess, DWORD dwFlags, DWORD_PTR dwContext = 0)
557 {
558 return ::FtpOpenFile(m_handle, lpszFileName, dwAccess, dwFlags, dwContext);
559 }
560
583 bool PutFile(LPCTSTR lpszLocalFile, LPCTSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext)
584 {
585 return !! ::FtpPutFile(m_handle, lpszLocalFile, lpszNewRemoteFile, dwFlags, dwContext);
586 }
587
596 bool RemoveDirectory(LPCTSTR lpszDirName)
597 {
598 return !! ::FtpRemoveDirectory(m_handle, lpszDirName);
599 }
600
610 bool RenameFile(LPCTSTR lpszOldFileName, LPCTSTR lpszNewFileName)
611 {
612 return !! ::FtpRenameFile(m_handle, lpszOldFileName, lpszNewFileName);
613 }
614
623 bool SetCurrentDirectory(LPCTSTR lpszDirName)
624 {
625 return !! ::FtpSetCurrentDirectory(m_handle, lpszDirName);
626 }
627
634 static bool AttemptConnect(void)
635 {
636 return ::InternetAttemptConnect(0) == ERROR_SUCCESS;
637 }
638
660 static bool CanonicalizeUrl(LPTSTR _lpszDstUrl, DWORD& _dwDstSize, LPCTSTR lpszUrl, DWORD dwFlags, LPCTSTR lpszRelativeUrl = NULL)
661 {
662 BOOL boRc = FALSE;
663 if ( lpszRelativeUrl == NULL )
664 {
665 boRc = ::InternetCanonicalizeUrl(lpszUrl, _lpszDstUrl, &_dwDstSize, dwFlags);
666 }
667 else
668 {
669 boRc = ::InternetCombineUrl(lpszUrl, lpszRelativeUrl, _lpszDstUrl, &_dwDstSize, dwFlags);
670 }
671 return !! boRc;
672 }
673};
674
675
676
677}; // TNB
678
679
680
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
動的関数管理関係のヘッダ
ポインタハンドル関係のヘッダ
文字列情報配列管理関係のヘッダ
動的関数管理クラステンプレート
INTERNETハンドル管理クラス
CInternetHandle Connect(LPCTSTR lpszServerName, INTERNET_PORT tPort, LPCTSTR lpszUserName, LPCTSTR lpszPassword, DWORD dwService, DWORD dwFlags, DWORD_PTR dwContext=0)
[接続] 指定名サーバ接続.
bool GetFile(LPCTSTR lpszRemoteFile, LPCTSTR lpszLocalFile, BOOL fFailIfExists, DWORD dwFlagsAndAttributes, DWORD dwFlags, DWORD_PTR dwContext=0) const
[取得] ローカルへ読込み.
CInternetHandle OpenFile(LPCTSTR lpszFileName, DWORD dwAccess, DWORD dwFlags, DWORD_PTR dwContext=0)
[取得] ファイルオープン.
bool HttpQueryInfo(DWORD dwInfoLevel, LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
[取得] HTTPリクエストに関連する情報の取得.
CInternetHandle(HINTERNET h=NULL)
コンストラクタ
DWORD GetFileSize(LPDWORD _lpdwFileSizeHigh) const
[取得] ファイルサイズ取得.
CInternetHandle FindFirstFile(LPCTSTR lpszSearchFile, LPWIN32_FIND_DATA lpFindFileData, DWORD dwFlags, DWORD_PTR dwContext=0) const
[検索] 検索開始.
bool FindNextFile(LPVOID pFindData) const
[検索] 検索続行.
static bool CanonicalizeUrl(LPTSTR _lpszDstUrl, DWORD &_dwDstSize, LPCTSTR lpszUrl, DWORD dwFlags, LPCTSTR lpszRelativeUrl=NULL)
[変換] URL変換.
bool RenameFile(LPCTSTR lpszOldFileName, LPCTSTR lpszNewFileName)
[変更] 名前変更.
bool QueryOption(DWORD dwOption, LPVOID _lpBuffer, LPDWORD _lpdwBufferLength) const
[取得] オプション情報取得.
CInternetHandle HttpOpenRequest(LPCTSTR lpszVerb, LPCTSTR lpszObjectName, LPCTSTR lpszVersion, LPCTSTR lpszReferrer, DWORD dwFlags, DWORD_PTR dwContext=0)
[作成] HTTPリクエストの作成.
INTERNET_STATUS_CALLBACK SetStatusCallback(INTERNET_STATUS_CALLBACK pInetProc)
[設定] コールバック設定.
bool Open(LPCTSTR lpszAgent, DWORD dwAccessType, LPCTSTR lpszProxy=NULL, LPCTSTR lpszProxyBypass=NULL, DWORD dwFlags=0)
[設定] 初期化.
bool GetCurrentDirectory(LPTSTR _lpszCurrentDirectory, LPDWORD _lpdwLength) const
[取得] カレントディレクトリ取得.
CInternetHandle OpenUrl(LPCTSTR lpszUrl, LPCTSTR lpszHeaders, DWORD dwHeadersLength, DWORD dwFlags, DWORD_PTR dwContext=0)
[接続] 指定URL接続.
bool SetCurrentDirectory(LPCTSTR lpszDirName)
[設定] カレントディレクトリ設定.
bool IsValid(void) const
[確認] ハンドル有効確認
static bool AttemptConnect(void)
[調査] インターネット利用可能調査.
bool ReadFile(LPVOID _lpBuffer, DWORD dwNumberOfBytesToRead, LPDWORD _lpdwNumberOfBytesRead) const
[取得] ファイル読み込み.
bool WriteFile(LPCVOID lpBuffer, DWORD dwNumberOfBytesToWrite, LPDWORD lpdwNumberOfBytesWritten)
[出力] ファイル書き込み.
bool SetOption(DWORD dwOption, LPVOID lpBuffer, DWORD dwBufferLength)
[設定] オプション情報設定
CInternetHandle & operator=(const CInternetHandle &other)
[代入] コピーオペレータ
bool CreateDirectory(LPCTSTR lpszDirName)
[作成] 指定ディレクトリ作成.
void Close(void)
[設定] クローズ.
bool HttpSendRequest(LPCTSTR lpszHeaders, DWORD dwHeadersLength, LPVOID lpOptional, DWORD dwOptionalLength)
[送信] HTTPリクエストを送信.
DWORD SetFilePointer(LONG lDistanceToMove, LONG *pReserved, DWORD dwMoveMethod, DWORD_PTR dwContext=0) const
[設定] ファイルポインタ設定.
CInternetHandle HttpOpenRequest(LPCTSTR lpszVerb, LPCTSTR lpszObjectName, LPCTSTR lpszVersion, LPCTSTR lpszReferrer, const CStrVector &acceptTypes, DWORD dwFlags, DWORD_PTR dwContext=0)
[作成] HTTPリクエストの作成.
bool PutFile(LPCTSTR lpszLocalFile, LPCTSTR lpszNewRemoteFile, DWORD dwFlags, DWORD_PTR dwContext)
[送信] ローカルからファイル送信.
CInternetHandle(const CInternetHandle &other)
コピーコンストラクタ
bool RemoveDirectory(LPCTSTR lpszDirName)
[削除] ディレクトリ削除.
bool DeleteFile(LPCTSTR lpszFileName)
[削除] 指定ファイル削除.
NULLポインタ例外
Definition: TnbException.h:172
bool IsNull(void) const
[確認] NULLチェック
void Null(void)
[設定] 開放.
const TYP * ReferBuffer(void) const
[取得] バッファ参照.
Definition: TnbStr.h:380
void OnCatch(void) const
[表示] 内容表示
Definition: TnbException.h:69
virtual size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbVector.h:368
virtual const TYP * ReferBuffer(void) const
[取得] データアドレス取得
Definition: TnbVector.h:664
ワークメモリテンプレート.
Definition: TnbDef.h:633
TNB Library
Definition: TnbDoxyTitle.txt:2
TNB::CPointerHandleBaseT< HINTERNET, TPhInternetCloseHandle > CInternetHandleHandle
HINTERNET型ハンドルハンドラ―クラス