📄 inet.h
字号:
/*
Filename: INET.H
Description: Defines some wrapper classes for wininet functions
Date: 25/06/2005
Copyright (c) 2005 by Gilad Novik. (Web: http://gilad.gsetup.com, Email: gilad@gsetup.com)
All rights reserved.
Copyright / Usage Details
-------------------------
You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise)
when your product is released in binary form. You are allowed to modify the source code in any way you want
except you cannot modify the copyright details at the top of the module. If you want to distribute source
code with your application, then you are only allowed to distribute versions released by the author. This is
to maintain a single distribution point for the source code.
*/
#pragma once
#include <wininet.h>
#pragma comment(lib, "wininet.lib")
class CInternetException;
class CInternetStatucCallback;
class CInternetHandle;
class CInternetSession;
class CInternetConnection;
class CHttpConnection;
class CFtpConnection;
class CInternetFile;
class CHttpFile;
class CHttpsFile;
class CFtpFile;
class CFtpFindFile;
class CInternetException
{
public:
explicit CInternetException(HINTERNET hHandle,LPCTSTR szMessage,...) : m_hHandle(hHandle)
{
m_dwError=GetLastError();
#if defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
DWORD dwLength=0;
InternetGetLastResponseInfo(&m_dwInternetError,NULL,&dwLength);
if (dwLength)
{
InternetGetLastResponseInfo(&m_dwInternetError,m_szInternetMessage.GetBuffer(dwLength-1),&dwLength);
m_szInternetMessage.ReleaseBuffer();
}
else
m_szInternetMessage=_T("Unknown error");
#else
DWORD dwLength=sizeof(m_szInternetMessage)/sizeof(TCHAR);
if (!InternetGetLastResponseInfo(&m_dwInternetError,m_szInternetMessage,&dwLength) && GetLastError()!=ERROR_INSUFFICIENT_BUFFER)
lstrcpy(m_szInternetMessage,_T("Unknown error"));
#endif
va_list va;
va_start(va,szMessage);
#if defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
m_szMessage.FormatV(szMessage,va);
#else
_vsntprintf(m_szMessage,sizeof(m_szMessage)/sizeof(TCHAR)-1,szMessage,va);
#endif
va_end(va);
}
virtual ~CInternetException()
{
}
HINTERNET GetInternetHandle() const
{
return m_hHandle;
}
DWORD GetErrorCode() const
{
return m_dwError;
}
DWORD GetInternetErrorCode() const
{
return m_dwInternetError;
}
LPCTSTR GetErrorMessage() const
{
return m_szMessage;
}
LPCTSTR GetInternetErrorMessage() const
{
return m_szInternetMessage;
}
protected:
HINTERNET m_hHandle;
DWORD m_dwError,m_dwInternetError;
#if defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
_CSTRING_NS::CString m_szMessage,m_szInternetMessage;
#else
TCHAR m_szMessage[512],m_szInternetMessage[512];
#endif
};
class CInternetStatucCallback
{
public:
virtual void OnCallback(CInternetHandle& Handle,HINTERNET hInternet,DWORD dwStatus,LPVOID pStatusInformation,DWORD dwStatusInformationLength)
{
switch (dwStatus)
{
case INTERNET_STATUS_CLOSING_CONNECTION:
OnClosingConnection(Handle,hInternet);
break;
case INTERNET_STATUS_CONNECTED_TO_SERVER:
OnConnected(Handle,hInternet,(SOCKADDR*)pStatusInformation);
break;
case INTERNET_STATUS_CONNECTING_TO_SERVER:
OnConnecting(Handle,hInternet,(SOCKADDR*)pStatusInformation);
break;
case INTERNET_STATUS_CONNECTION_CLOSED:
OnConnectionClosed(Handle,hInternet);
break;
case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
break;
case INTERNET_STATUS_DETECTING_PROXY:
OnDetectingProxy(Handle,hInternet);
break;
case INTERNET_STATUS_HANDLE_CLOSING:
OnHandleClosing(Handle,hInternet);
break;
case INTERNET_STATUS_HANDLE_CREATED:
OnHandleCreated(Handle,hInternet,(INTERNET_ASYNC_RESULT*)pStatusInformation);
break;
case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
OnIntermediateResponse(Handle,hInternet);
break;
case INTERNET_STATUS_NAME_RESOLVED:
OnNameResolved(Handle,hInternet,(LPCTSTR)pStatusInformation);
break;
case INTERNET_STATUS_PREFETCH:
break;
case INTERNET_STATUS_RECEIVING_RESPONSE:
OnReceivingResponse(Handle,hInternet);
break;
case INTERNET_STATUS_REDIRECT:
OnRedirect(Handle,hInternet,(LPCTSTR)pStatusInformation);
break;
case INTERNET_STATUS_REQUEST_COMPLETE:
OnRequestComplete(Handle,hInternet,(INTERNET_ASYNC_RESULT*)pStatusInformation);
break;
case INTERNET_STATUS_REQUEST_SENT:
OnRequestSent(Handle,hInternet,*(DWORD*)pStatusInformation);
break;
case INTERNET_STATUS_RESOLVING_NAME:
OnResolvingName(Handle,hInternet,(LPCTSTR)pStatusInformation);
break;
case INTERNET_STATUS_RESPONSE_RECEIVED:
OnResponseReceived(Handle,hInternet,*(DWORD*)pStatusInformation);
break;
case INTERNET_STATUS_SENDING_REQUEST:
OnSendingRequest(Handle,hInternet);
break;
case INTERNET_STATUS_STATE_CHANGE:
OnStateChange(Handle,hInternet,*(DWORD*)pStatusInformation);
break;
}
}
virtual void OnClosingConnection(CInternetHandle& Handle,HINTERNET hInternet)
{
}
virtual void OnConnected(CInternetHandle& Handle,HINTERNET hInternet,const SOCKADDR* pAddress)
{
}
virtual void OnConnecting(CInternetHandle& Handle,HINTERNET hInternet,const SOCKADDR* pAddress)
{
}
virtual void OnConnectionClosed(CInternetHandle& Handle,HINTERNET hInternet)
{
}
virtual void OnDetectingProxy(CInternetHandle& Handle,HINTERNET hInternet)
{
}
virtual void OnHandleClosing(CInternetHandle& Handle,HINTERNET hInternet)
{
}
virtual void OnHandleCreated(CInternetHandle& Handle,HINTERNET hInternet,const INTERNET_ASYNC_RESULT* pResult)
{
DWORD_PTR dwContext=(DWORD_PTR)&Handle;
InternetSetOption((HINTERNET)pResult->dwResult,INTERNET_OPTION_CONTEXT_VALUE,&dwContext,sizeof(DWORD_PTR));
}
virtual void OnIntermediateResponse(CInternetHandle& Handle,HINTERNET hInternet)
{
}
virtual void OnNameResolved(CInternetHandle& Handle,HINTERNET hInternet,LPCTSTR szName)
{
}
virtual void OnReceivingResponse(CInternetHandle& Handle,HINTERNET hInternet)
{
}
virtual void OnRedirect(CInternetHandle& Handle,HINTERNET hInternet,LPCTSTR szURL)
{
}
virtual void OnRequestComplete(CInternetHandle& Handle,HINTERNET hInternet,const INTERNET_ASYNC_RESULT* pResult)
{
}
virtual void OnRequestSent(CInternetHandle& Handle,HINTERNET hInternet,DWORD dwSize)
{
}
virtual void OnResolvingName(CInternetHandle& Handle,HINTERNET hInternet,LPCTSTR szName)
{
}
virtual void OnResponseReceived(CInternetHandle& Handle,HINTERNET hInternet,DWORD dwSize)
{
}
virtual void OnSendingRequest(CInternetHandle& Handle,HINTERNET hInternet)
{
}
virtual void OnStateChange(CInternetHandle& Handle,HINTERNET hInternet,DWORD dwFlags)
{
}
};
class CInternetHandle
{
public:
CInternetHandle() : m_hHandle(NULL), m_pCallback(NULL)
{
}
virtual ~CInternetHandle()
{
Close();
}
void Close()
{
if (m_hHandle)
{
InternetCloseHandle(m_hHandle);
m_hHandle=NULL;
}
}
operator HINTERNET() const
{
return m_hHandle;
}
void SetOption(DWORD dwOption,LPVOID pBuffer,DWORD dwLength) throw(...)
{
ATLASSERT(m_hHandle);
if (!InternetSetOption(m_hHandle,dwOption,pBuffer,dwLength))
throw CInternetException(m_hHandle,_T("Failed to set an internet option (%u)"),dwOption);
}
void SetOption(DWORD dwOption,DWORD dwValue) throw(...)
{
SetOption(dwOption,&dwValue,sizeof(dwValue));
}
void SetOption(DWORD dwOption,LPCTSTR szBuffer) throw(...)
{
SetOption(dwOption,(LPVOID)szBuffer,lstrlen(szBuffer));
}
void QueryOption(DWORD dwOption,LPVOID pBuffer,LPDWORD pdwLength) throw(...)
{
ATLASSERT(m_hHandle);
if (!InternetQueryOption(m_hHandle,dwOption,pBuffer,pdwLength))
throw CInternetException(m_hHandle,_T("Failed to query an internet option (%u)"),dwOption);
}
void QueryOption(DWORD dwOption,DWORD& dwValue) throw(...)
{
DWORD dwLength=sizeof(dwValue);
QueryOption(dwOption,&dwValue,&dwLength);
}
#if defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
void QueryOption(DWORD dwOption,_CSTRING_NS::CString& szBuffer) throw(...)
{
ATLASSERT(m_hHandle);
DWORD dwLength=0;
if (InternetQueryOption(m_hHandle,dwOption,NULL,&dwLength) || GetLastError()==ERROR_INSUFFICIENT_BUFFER)
{
BOOL bResult=InternetQueryOption(m_hHandle,dwOption,szBuffer.GetBuffer(dwLength/sizeof(TCHAR)-1),&dwLength);
szBuffer.ReleaseBuffer();
if (bResult)
return;
}
throw CInternetException(m_hHandle,_T("Failed to query an internet option (%u)"),dwOption);
}
#endif
void SetCallback(CInternetStatucCallback* pCallback) throw(...)
{
DWORD_PTR dwContext=(DWORD_PTR)this;
SetOption(INTERNET_OPTION_CONTEXT_VALUE,&dwContext,sizeof(DWORD_PTR));
InternetSetStatusCallback(m_hHandle,(m_pCallback=pCallback) ? InternetStatusCallback : NULL);
}
protected:
HINTERNET m_hHandle;
CInternetStatucCallback* m_pCallback;
private:
static void CALLBACK InternetStatusCallback(HINTERNET hInternet,DWORD_PTR dwContext,DWORD dwInternetStatus,LPVOID lpvStatusInformation,DWORD dwStatusInformationLength)
{
CInternetHandle* pHandle=NULL;
DWORD dwLength=sizeof(DWORD_PTR);
if (InternetQueryOption(hInternet,INTERNET_OPTION_CONTEXT_VALUE,&pHandle,&dwLength) && pHandle->m_pCallback)
pHandle->m_pCallback->OnCallback(*pHandle,hInternet,dwInternetStatus,lpvStatusInformation,dwStatusInformationLength);
}
friend class CFtpConnection;
};
class CInternetSession : public CInternetHandle
{
public:
CInternetSession(LPCTSTR szUserAgent=NULL,DWORD dwAccessType=INTERNET_OPEN_TYPE_PRECONFIG,LPCTSTR lpszProxy=NULL,LPCTSTR lpszProxyBypass=NULL,DWORD dwFlags=0) throw(...)
{
TCHAR szFilename[MAX_PATH];
if (!szUserAgent)
{
GetModuleFileName(NULL,szFilename,sizeof(szFilename)/sizeof(TCHAR));
PathRemoveExtension(szFilename);
szUserAgent=PathFindFileName(szFilename);
}
if (!(m_hHandle=InternetOpen(szUserAgent,dwAccessType,lpszProxy,lpszProxyBypass,dwFlags)))
throw CInternetException(m_hHandle,_T("Failed to open an internet session"));
}
};
class CInternetConnection : public CInternetHandle
{
public:
CInternetConnection(HINTERNET hSession,LPCTSTR szServer,INTERNET_PORT nPort,LPCTSTR szUsername,LPCTSTR szPassword,DWORD dwService,DWORD dwFlags) throw(...)
{
ATLASSERT(hSession);
if (!(m_hHandle=InternetConnect(hSession,szServer,nPort,szUsername,szPassword,dwService,dwFlags,(DWORD_PTR)(CInternetHandle*)this)))
throw CInternetException(m_hHandle,_T("Failed to connect to server (%s:%u)"),szServer,nPort);
}
};
class CHttpConnection : public CInternetConnection
{
public:
CHttpConnection(HINTERNET hSession,LPCTSTR szServer,INTERNET_PORT nPort=INTERNET_DEFAULT_HTTP_PORT,LPCTSTR szUsername=NULL,LPCTSTR szPassword=NULL,DWORD dwFlags=0) throw(...) : CInternetConnection(hSession,szServer,nPort,szUsername,szPassword,INTERNET_SERVICE_HTTP,dwFlags)
{
}
};
class CFtpConnection : public CInternetConnection
{
public:
CFtpConnection(HINTERNET hSession,LPCTSTR szServer,INTERNET_PORT nPort=INTERNET_DEFAULT_FTP_PORT,LPCTSTR szUsername=NULL,LPCTSTR szPassword=NULL,DWORD dwFlags=INTERNET_FLAG_PASSIVE) throw(...) : CInternetConnection(hSession,szServer,nPort,szUsername,szPassword,INTERNET_SERVICE_FTP,dwFlags)
{
}
void SetCurrentDirectory(LPCTSTR szDirectory) throw(...)
{
ATLASSERT(m_hHandle);
if (!FtpSetCurrentDirectory(m_hHandle,szDirectory))
throw CInternetException(m_hHandle,_T("Failed to set current directory (%s)"),szDirectory);
}
void GetCurrentDirectory(LPTSTR szDirectory,LPDWORD pdwLength) throw(...)
{
ATLASSERT(m_hHandle);
if (!FtpGetCurrentDirectory(m_hHandle,szDirectory,pdwLength))
throw CInternetException(m_hHandle,_T("Failed to get current directory"));
}
#if defined(_WTL_USE_CSTRING) || defined(__ATLSTR_H__)
void GetCurrentDirectory(_CSTRING_NS::CString& szDirectory) throw(...)
{
ATLASSERT(m_hHandle);
DWORD dwLength=0;
if (FtpGetCurrentDirectory(m_hHandle,NULL,&dwLength) || GetLastError()==ERROR_INSUFFICIENT_BUFFER)
{
BOOL bResult=FtpGetCurrentDirectory(m_hHandle,szDirectory.GetBuffer(dwLength/sizeof(TCHAR)-1),&dwLength);
szDirectory.ReleaseBuffer();
if (bResult)
return;
}
throw CInternetException(m_hHandle,_T("Failed to get current directory"));
}
#endif
void CreateDirectory(LPCTSTR szDirectory) throw(...)
{
ATLASSERT(m_hHandle);
if (!FtpCreateDirectory(m_hHandle,szDirectory))
throw CInternetException(m_hHandle,_T("Failed to create directory (%s"),szDirectory);
}
void RemoveDirectory(LPCTSTR szDirectory) throw(...)
{
ATLASSERT(m_hHandle);
if (!FtpRemoveDirectory(m_hHandle,szDirectory))
throw CInternetException(m_hHandle,_T("Failed to remove directory (%s"),szDirectory);
}
void Rename(LPCTSTR szExisting,LPCTSTR szNew) throw(...)
{
ATLASSERT(m_hHandle);
if (!FtpRenameFile(m_hHandle,szExisting,szNew))
throw CInternetException(m_hHandle,_T("Failed to rename file (%s -> %s"),szExisting,szNew);
}
void Remove(LPCTSTR szFilename) throw(...)
{
ATLASSERT(m_hHandle);
if (!FtpDeleteFile(m_hHandle,szFilename))
throw CInternetException(m_hHandle,_T("Failed to remove file (%s)"),szFilename);
}
void Upload(LPCTSTR szLocal,LPCTSTR szRemote,DWORD dwFlags=FTP_TRANSFER_TYPE_UNKNOWN) throw(...)
{
ATLASSERT(m_hHandle);
if (!FtpPutFile(m_hHandle,szLocal,szRemote,dwFlags,(DWORD_PTR)(CInternetHandle*)this))
throw CInternetException(m_hHandle,_T("Failed to upload file (%s -> %s)"),szLocal,szRemote);
}
void Download(LPCTSTR szRemote,LPCTSTR szLocal,BOOL bFailIfExists=TRUE,DWORD dwAttributes=FILE_ATTRIBUTE_NORMAL,DWORD dwFlags=FTP_TRANSFER_TYPE_UNKNOWN) throw(...)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -