📄 inet.cpp
字号:
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT(AfxIsValidString(pstrDirName));
return FtpSetCurrentDirectory(m_hConnection, pstrDirName);
}
BOOL CFtpConnection::GetCurrentDirectory(LPTSTR pstrDirName,
LPDWORD lpdwLen) const
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT(AfxIsValidAddress(pstrDirName, *lpdwLen));
ASSERT(lpdwLen != 0);
return FtpGetCurrentDirectory(m_hConnection, pstrDirName, lpdwLen);
}
BOOL CFtpConnection::GetCurrentDirectoryAsURL(CString& strDirName) const
{
CString strDirectory;
if (!GetCurrentDirectory(strDirectory))
return FALSE;
strDirName = _afxURLftp;
strDirName += GetServerName();
if (strDirectory[0] != '/')
strDirName += '/';
strDirName += strDirectory;
return TRUE;
}
BOOL CFtpConnection::GetCurrentDirectoryAsURL(LPTSTR pstrName,
LPDWORD lpdwLen) const
{
ASSERT(lpdwLen != NULL);
ASSERT_POINTER(lpdwLen, DWORD);
ASSERT(AfxIsValidAddress(pstrName, *lpdwLen));
ASSERT(*lpdwLen != 0);
CString strTemp;
if (lpdwLen == NULL || !GetCurrentDirectoryAsURL(strTemp))
return FALSE;
if (pstrName == NULL)
*lpdwLen = strTemp.GetLength();
else
lstrcpyn(pstrName, (LPCTSTR) strTemp, max(0, *lpdwLen -1));
return TRUE;
}
BOOL CFtpConnection::GetCurrentDirectory(CString& strDirName) const
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
DWORD dwLen = INTERNET_MAX_PATH_LENGTH;
LPTSTR pstrTarget = strDirName.GetBufferSetLength(dwLen);
BOOL bRet = FtpGetCurrentDirectory(m_hConnection, pstrTarget, &dwLen);
if (bRet)
strDirName.ReleaseBuffer(dwLen);
else
strDirName.ReleaseBuffer(0);
return bRet;
}
CInternetFile* CFtpConnection::OpenFile(LPCTSTR pstrFileName,
DWORD dwAccess /* = GENERIC_READ */,
DWORD dwFlags /* = FTP_TRANSFER_TYPE_BINARY */,
DWORD dwContext /* = 1 */)
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT(dwAccess != (GENERIC_READ | GENERIC_WRITE));
ASSERT(dwAccess == GENERIC_READ || dwAccess == GENERIC_WRITE);
ASSERT(AfxIsValidString(pstrFileName));
HINTERNET hFile;
if (dwContext == 1)
dwContext = m_dwContext;
hFile = FtpOpenFile(m_hConnection, pstrFileName, dwAccess,
dwFlags, dwContext);
if (hFile == NULL)
AfxThrowInternetException(dwContext);
CInternetFile* pFile = new CInternetFile(hFile, pstrFileName, this,
(dwAccess == GENERIC_READ));
return pFile;
}
BOOL CFtpConnection::PutFile(LPCTSTR pstrLocalFile, LPCTSTR pstrRemoteFile,
DWORD dwFlags /* = FTP_TRANSFER_TYPE_BINARY */,
DWORD dwContext /* = 1 */)
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT(AfxIsValidString(pstrRemoteFile));
ASSERT(AfxIsValidString(pstrLocalFile));
ASSERT((dwFlags & INTERNET_FLAG_ASYNC) == 0);
if (dwContext == 1)
dwContext = m_dwContext;
return FtpPutFile(m_hConnection, pstrLocalFile, pstrRemoteFile,
dwFlags, dwContext);
}
BOOL CFtpConnection::GetFile(LPCTSTR pstrRemoteFile, LPCTSTR pstrLocalFile,
BOOL bFailIfExists /* = TRUE */,
DWORD dwAttributes /* = FILE_ATTRIBUTE_NORMAL */,
DWORD dwFlags /* = FTP_TRANSFER_TYPE_BINARY */, DWORD dwContext /* = 1 */)
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT(AfxIsValidString(pstrRemoteFile));
ASSERT(AfxIsValidString(pstrLocalFile));
ASSERT(!(dwAttributes & FILE_ATTRIBUTE_DIRECTORY));
ASSERT((dwFlags & INTERNET_FLAG_ASYNC) == 0);
if (dwContext == 1)
dwContext = m_dwContext;
return FtpGetFile(m_hConnection, pstrRemoteFile, pstrLocalFile,
bFailIfExists, dwAttributes, dwFlags, dwContext);
}
#ifdef _DEBUG
void CFtpConnection::Dump(CDumpContext& dc) const
{
CInternetConnection::Dump(dc);
dc << "\nm_strServerName = " << m_strServerName;
}
void CFtpConnection::AssertValid() const
{
ASSERT(m_pSession != NULL);
if (m_hConnection != NULL)
{
ASSERT(AfxGetInternetHandleType(m_hConnection)
== INTERNET_HANDLE_TYPE_CONNECT_FTP);
}
}
#endif
/////////////////////////////////////////////////////////////////////////////
// CGopherConnection
CGopherConnection::~CGopherConnection()
{
}
CGopherConnection::CGopherConnection(CInternetSession* pSession,
LPCTSTR pstrServer, LPCTSTR pstrUserName /* = NULL */,
LPCTSTR pstrPassword /* = NULL */, DWORD dwContext /* = 0 */,
INTERNET_PORT nPort /* = INTERNET_INVALID_PORT_NUMBER */)
: CInternetConnection(pSession, pstrServer, nPort, dwContext)
{
ASSERT(pSession != NULL);
ASSERT_KINDOF(CInternetSession, pSession);
ASSERT(AfxIsValidString(pstrServer));
m_hConnection = InternetConnect((HINTERNET) *pSession, pstrServer,
nPort, pstrUserName, pstrPassword, INTERNET_SERVICE_GOPHER,
0, m_dwContext);
if (m_hConnection == NULL)
AfxThrowInternetException(m_dwContext);
else
_afxSessionMap.SetAt(m_hConnection, m_pSession);
}
CGopherConnection::CGopherConnection(CInternetSession* pSession,
HINTERNET hConnected, LPCTSTR pstrServer, DWORD dwContext)
: CInternetConnection(pSession, pstrServer,
INTERNET_INVALID_PORT_NUMBER, dwContext)
{
ASSERT(pSession != NULL);
ASSERT(AfxIsValidString(pstrServer));
BOOL bBadType = FALSE;
if (AfxGetInternetHandleType(hConnected) != INTERNET_HANDLE_TYPE_CONNECT_GOPHER)
{
ASSERT(FALSE); // used the wrong handle type
bBadType = TRUE;
}
m_hConnection = hConnected;
if (m_hConnection == NULL || bBadType)
AfxThrowInternetException(m_dwContext);
else
_afxSessionMap.SetAt(m_hConnection, m_pSession);
}
CGopherLocator CGopherConnection::CreateLocator(LPCTSTR pstrLocator)
{
CGopherLocator ret(pstrLocator, lstrlen(pstrLocator));
return ret;
}
CGopherLocator CGopherConnection::CreateLocator(LPCTSTR pstrServerName,
LPCTSTR pstrDisplayString, LPCTSTR pstrSelectorString, DWORD dwGopherType,
INTERNET_PORT nPort /* = INTERNET_INVALID_PORT_NUMBER */)
{
TCHAR szLocator[MAX_GOPHER_LOCATOR_LENGTH];
DWORD dwLocLen = MAX_GOPHER_LOCATOR_LENGTH;
ASSERT(AfxIsValidString(pstrDisplayString));
ASSERT(AfxIsValidString(pstrServerName));
ASSERT(AfxIsValidString(pstrSelectorString));
if (!GopherCreateLocator(pstrServerName, nPort,
pstrDisplayString, pstrSelectorString, dwGopherType,
szLocator, &dwLocLen))
AfxThrowInternetException(0);
CGopherLocator ret(szLocator, dwLocLen);
return ret;
}
CGopherLocator CGopherConnection::CreateLocator(
LPCTSTR pstrDisplayString, LPCTSTR pstrSelectorString, DWORD dwGopherType)
{
TCHAR szLocator[MAX_GOPHER_LOCATOR_LENGTH];
DWORD dwLocLen = MAX_GOPHER_LOCATOR_LENGTH;
ASSERT(AfxIsValidString(pstrDisplayString));
ASSERT(AfxIsValidString(pstrSelectorString));
if (!GopherCreateLocator(m_strServerName, m_nPort,
pstrDisplayString, pstrSelectorString, dwGopherType,
szLocator, &dwLocLen))
AfxThrowInternetException(m_dwContext);
CGopherLocator ret(szLocator, dwLocLen);
return ret;
}
BOOL CGopherConnection::GetAttribute(CGopherLocator& refLocator,
CString strRequestedAttributes, CString& strResult)
{
DWORD dwLen = 4*MIN_GOPHER_ATTRIBUTE_LENGTH; // more than the minimum
BOOL bRet;
LPTSTR pstrResult = strResult.GetBuffer(dwLen);
if (!GopherGetAttribute(m_hConnection, (LPCTSTR) refLocator,
pstrResult, NULL, dwLen, &dwLen,
NULL, m_dwContext))
{
bRet = FALSE;
strResult.ReleaseBuffer(0);
}
else
{
bRet = TRUE;
strResult.ReleaseBuffer(dwLen);
}
return bRet;
}
CGopherFile* CGopherConnection::OpenFile(CGopherLocator& refLocator,
DWORD dwFlags /* = 0 */, LPCTSTR pstrView /* = NULL */,
DWORD dwContext /* = 1 */)
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
HINTERNET hFile;
if (dwContext == 1)
dwContext = m_dwContext;
hFile = GopherOpenFile(m_hConnection, (LPCTSTR) refLocator, pstrView,
dwFlags, dwContext);
if (hFile == NULL)
AfxThrowInternetException(dwContext);
CGopherFile* pFile = new CGopherFile(hFile, refLocator, this);
return pFile;
}
void CGopherConnection::Close()
{
CInternetConnection::Close();
}
#ifdef _DEBUG
void CGopherConnection::Dump(CDumpContext& dc) const
{
CInternetConnection::Dump(dc);
dc << "\nm_strServerName = " << m_strServerName;
}
void CGopherConnection::AssertValid() const
{
ASSERT(m_pSession != NULL);
if (m_hConnection != NULL)
{
ASSERT(AfxGetInternetHandleType(m_hConnection)
== INTERNET_HANDLE_TYPE_CONNECT_GOPHER);
}
}
#endif
/////////////////////////////////////////////////////////////////////////////
// CHttpConnection
CHttpConnection::~CHttpConnection()
{
}
CHttpConnection::CHttpConnection(CInternetSession* pSession,
HINTERNET hConnected, LPCTSTR pstrServer, DWORD dwContext /* = 0 */)
: CInternetConnection(pSession, pstrServer, INTERNET_INVALID_PORT_NUMBER, dwContext)
{
ASSERT(pSession != NULL);
ASSERT(AfxIsValidString(pstrServer));
BOOL bBadType = FALSE;
if (AfxGetInternetHandleType(hConnected) != INTERNET_HANDLE_TYPE_CONNECT_HTTP)
{
ASSERT(FALSE); // used the wrong handle type
bBadType = TRUE;
}
m_hConnection = hConnected;
if (m_hConnection == NULL || bBadType)
AfxThrowInternetException(m_dwContext, ERROR_INVALID_HANDLE);
else
_afxSessionMap.SetAt(m_hConnection, m_pSession);
}
CHttpConnection::CHttpConnection(CInternetSession* pSession,
LPCTSTR pstrServer,
INTERNET_PORT nPort /* = INTERNET_INVALID_PORT_NUMBER */,
LPCTSTR pstrUserName /* = NULL */,
LPCTSTR pstrPassword /* = NULL */, DWORD dwContext /* = 1 */)
: CInternetConnection(pSession, pstrServer, nPort, dwContext)
{
ASSERT(pSession != NULL);
ASSERT_KINDOF(CInternetSession, pSession);
ASSERT(AfxIsValidString(pstrServer));
m_hConnection = InternetConnect((HINTERNET) *pSession, pstrServer,
nPort, pstrUserName, pstrPassword, INTERNET_SERVICE_HTTP,
0, m_dwContext);
if (m_hConnection == NULL)
AfxThrowInternetException(m_dwContext);
else
_afxSessionMap.SetAt(m_hConnection, m_pSession);
}
CHttpConnection::CHttpConnection(CInternetSession* pSession,
LPCTSTR pstrServer, DWORD dwFlags,
INTERNET_PORT nPort /* = INTERNET_INVALID_PORT_NUMBER */,
LPCTSTR pstrUserName /* = NULL */,
LPCTSTR pstrPassword /* = NULL */,
DWORD dwContext /* = 1 */)
: CInternetConnection(pSession, pstrServer, nPort, dwContext)
{
ASSERT(pSession != NULL);
ASSERT_KINDOF(CInternetSession, pSession);
ASSERT((dwFlags & INTERNET_FLAG_ASYNC) == 0);
ASSERT(AfxIsValidString(pstrServer));
m_hConnection = InternetConnect((HINTERNET) *pSession, pstrServer,
nPort, pstrUserName, pstrPassword, INTERNET_SERVICE_HTTP,
dwFlags, m_dwContext);
if (m_hConnection == NULL)
AfxThrowInternetException(m_dwContext);
else
_afxSessionMap.SetAt(m_hConnection, m_pSession);
}
void CHttpConnection::Close()
{
CInternetConnection::Close();
}
CHttpFile* CHttpConnection::OpenRequest(LPCTSTR pstrVerb,
LPCTSTR pstrObjectName, LPCTSTR pstrReferer, DWORD dwContext,
LPCTSTR* ppstrAcceptTypes, LPCTSTR pstrVersion, DWORD dwFlags)
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT((dwFlags & INTERNET_FLAG_ASYNC) == 0);
if (dwContext == 1)
dwContext = m_dwContext;
if (pstrVersion == NULL)
pstrVersion = HTTP_VERSION;
HINTERNET hFile;
hFile = HttpOpenRequest(m_hConnection, pstrVerb, pstrObjectName,
pstrVersion, pstrReferer, ppstrAcceptTypes, dwFlags, dwContext);
CHttpFile* pRet = new CHttpFile(hFile, pstrVerb, pstrObjectName, this);
pRet->m_dwContext = dwContext;
return pRet;
}
CHttpFile* CHttpConnection::OpenRequest(int nVerb,
LPCTSTR pstrObjectName, LPCTSTR pstrReferer /* = NULL */, DWORD dwContext,
LPCTSTR* ppstrAcceptTypes /* = NULL */,
LPCTSTR pstrVersion /* = NULL */, DWORD dwFlags)
{
ASSERT_VALID(this);
ASSERT(m_hConnection != NULL);
ASSERT((dwFlags & INTERNET_FLAG_ASYNC) == 0);
ASSERT(AfxIsValidString(pstrObjectName));
ASSERT(nVerb >= _HTTP_VERB_MIN && nVerb <= _HTTP_VERB_MAX);
LPCTSTR pstrVerb;
if (nVerb >= _HTTP_VERB_MIN && nVerb <= _HTTP_VERB_MAX)
pstrVerb = szHtmlVerbs[nVerb];
else
pstrVerb = _T("");
return OpenRequest(pstrVerb, pstrObjectName, pstrReferer,
dwContext, ppstrAcceptTypes, pstrVersion, dwFlags);
}
#ifdef _DEBUG
void CHttpConnection::Dump(CDumpContext& dc) const
{
CInternetConnection::Dump(dc);
dc << "\nm_strServerName = " << m_strServerName;
}
void CHttpConnection::AssertValid() const
{
ASSERT(m_pSession != NULL);
if (m_hConnection != NULL)
{
ASSERT(AfxGetInternetHandleType(m_hConnection)
== INTERNET_HANDLE_TYPE_CONNECT_HTTP);
}
}
#endif
/////////////////////////////////////////////////////////////////////////////
// CHttpFile
CHttpFile::CHttpFile(HINTERNET hFile, HINTERNET hSession, LPCTSTR pstrObject,
LPCTSTR pstrServer, LPCTSTR pstrVerb, DWORD dwContext)
: CInternetFile(hFile, hSession, pstrObject, pstrServer, dwContext, TRUE),
m_strVerb(pstrVerb), m_strObject(pstrObject)
{
// caller must set _afxSessionMap!
ASSERT(AfxIsValidString(pstrVerb));
}
CHttpFile::CHttpFile(HINTERNET hFile, LPCTSTR pstrVerb, LPCTSTR pstrObject,
CHttpConnection* pConnection)
: CInternetFile(hFile, pstrObject, pConnection, TRUE),
m_strVerb(pstrVerb), m_strObject(pstrObject)
{
ASSERT(pstrVerb != NULL);
ASSERT(pstrObject != NULL);
ASSERT(pConnection != NULL);
ASSERT_VALID(pConnection);
}
CHttpFile::~CHttpFile()
{
}
void CHttpFile::Close()
{
CInternetFile::Close();
}
DWORD CHttpFile::ErrorDlg(CWnd* pParent /* = NULL */,
DWORD dwError /* = ERROR_INTERNET_INCORRECT_PASSWORD */,
DWORD dwFlags /* = FLAGS_ERROR_UI_FLAGS_GENERATE_DATA | FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS*/,
LPVOID* lppvData /* = NULL */)
{
ASSERT((dwFlags & INTERNET_FLAG_ASYNC) == 0);
HWND hWnd;
LPVOID lpEmpty;
LPVOID* lppvHolder;
if (lppvData == NULL)
{
lpEmpty = NULL;
lppvHolder = &lpEmpty;
}
else
lppvHolder = lppvData;
if (pParent == NULL || pParent->m_hWnd == NULL)
hWnd = GetDesktopWindow();
else
hWnd = pParent->m_hWnd;
return InternetErrorDlg(hWnd, m_hFile, dwError, dwFlags, lppvHolder);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -