?? crobotinternet.cpp
字號:
sErrMsg = "";
DWORD dwHttpStatus;
nResult = CROBOT_ERR_SUCCESS;
CString sMsg;
try
{
pSession = NULL;
pHttpFile = NULL;
pLocalFile = NULL;
sHeader = CreateStandardHeader();
nRead = 0;
pBuffer = new char[1024];
sResult = "";
sWorkingUrl = sURL;
/* Trim URL and add http:// if it contains no
protocol identifier */
sWorkingUrl.TrimLeft();
sWorkingUrl.TrimRight();
if (sWorkingUrl.Find(":") == -1)
{
if (sWorkingUrl.Left(1) == "/")
sWorkingUrl = "http:" + sWorkingUrl;
else
sWorkingUrl = "http://" + sWorkingUrl;
} // End if
// Check for invalid parameters
if (!(sURL.IsEmpty()) && !(sOutputFilespec.IsEmpty()))
{
// URL is not empty and output file spec is not empty
/* Check the URL - must be valid and
of the 'http:' service type */
DWORD dwServiceType;
CString sServer, sObject;
unsigned short nPort;
if (AfxParseURL(sWorkingUrl,
dwServiceType,
sServer,
sObject,
nPort))
{
if (dwServiceType == AFX_INET_SERVICE_HTTP)
{
pSession = new CInternetSession(
m_sUserAgent,
++m_nContext,
INTERNET_OPEN_TYPE_PRECONFIG);
pHttpFile = (CHttpFile*)
pSession->OpenURL(
sWorkingUrl,
1,
INTERNET_FLAG_RELOAD
| INTERNET_FLAG_DONT_CACHE
| INTERNET_FLAG_TRANSFER_BINARY
| INTERNET_FLAG_EXISTING_CONNECT,
sHeader,
-1L);
if (pHttpFile) /* OpenURL worked */
{
// Check the http return code
if (!pHttpFile->QueryInfoStatusCode(dwHttpStatus))
dwHttpStatus = 200;
if (dwHttpStatus >= 400)
{
switch(dwHttpStatus)
{
case 404:
nResult = CROBOT_ERR_NOT_FOUND;
break;
case 403:
case 407:
nResult = CROBOT_ERR_NOT_AUTHORIZED;
break;
default:
nResult = CROBOT_ERR_CONNECTION_FAILED;
break;
} // End switch
} // End if dwHttpStatus
else /* No error - read response data */
{
nResult = CROBOT_ERR_SUCCESS;
// Open local file for output
pLocalFile = new CFile;
pLocalFile->Open(sOutputFilespec,
CFile::modeWrite
| CFile::modeCreate);
// Read the data
do
{
nRead = pHttpFile->Read(pBuffer, 1023);
if (nRead != 0)
{
pBuffer[nRead] = 0;
pLocalFile->Write(pBuffer, nRead);
if (sResult == "") sResult = pBuffer;
} // End if
} while (nRead != 0);
} // End else
// Check for error embedded in return page
} // End if pHttpFile
else /* OpenURL failed */
{
nResult = CROBOT_ERR_CONNECTION_FAILED;
} // End else
} // End if
else
// Wrong service
nResult = CROBOT_ERR_INVALID_URL;
} // End if
else
// Invalid URL
nResult = CROBOT_ERR_INVALID_URL;
} // End if
else
// Empty URL or empty filespec
nResult = CROBOT_ERR_INVALID_PARAMETER;
} // End try
catch(CFileException* e)
{
int nErr = e->m_cause;
e->Delete();
delete pLocalFile;
pLocalFile = NULL;
// File exception occurred
nResult = CROBOT_ERR_FILE + nErr;
} // End catch
catch (CInternetException* e)
{
e->Delete();
// Internet exception occurred
nResult = CROBOT_ERR_CONNECTION_FAILED;
} // End catch
catch (...)
{
// Exception occurred
nResult = CROBOT_ERR_CONNECTION_FAILED;
} // End catch
// Clean up and exit function
if (pBuffer != NULL)
{
delete pBuffer;
pBuffer = NULL;
} // End if
if (pHttpFile != NULL)
{
pHttpFile->Close();
delete pHttpFile;
} // End if
if (pLocalFile != NULL)
{
pLocalFile->Close();
delete pLocalFile;
} // End if
if (pSession != NULL)
{
pSession->Close();
delete pSession;
} // End if
sErrMsg = ErrorMessage(nResult);
if (nResult == CROBOT_ERR_SUCCESS)
return true;
else
return false;
}
// --------------------------------------------------------------
// **************** public
// * *
// * httpHeader *
// * *
// ****************
// Function: Retrieves a server header for a URL and returns it
// in CString form.
// See also: Similar function httpHeaderFields.
//
// Inputs: sURL - The target URL
// (example: "www.mysite.com")
//
// Outputs: <function_result> - True if header was successfully
// retrieved, false otherwise.
// sResponse - The header returned by the server
// nResult - completion code; 0 = success,
// n = error (defined in CRobot.h)
// sErrMsg - the error message, if nResult != 0
BOOL CRobotInternet::httpHeader(const CString& sUrl,
CString& sResponse,
int& nResult,
CString& sErrMsg)
{
CInternetSession* pSession;
CHttpConnection* pConnection;
CHttpFile* pHttpFile;
CString sHeader;
int nRead;
LPSTR pBuffer;
CString sResult;
CString sWorkingUrl;
CString sMsg;
sErrMsg = "";
nResult = CROBOT_ERR_CONNECTION_FAILED;
try
{
pSession = NULL;
pConnection = NULL;
pHttpFile = NULL;
pHttpFile = NULL;
sHeader = CreateStandardHeader();
nRead = 0;
pBuffer = new char[1024];
sResult = "";
sWorkingUrl = sUrl;
/* Trim URL and add http:// if it contains no
protocol identifier */
sWorkingUrl.TrimLeft();
sWorkingUrl.TrimRight();
if (sWorkingUrl.Find(":") == -1)
{
if (sWorkingUrl.Left(1) == "/")
sWorkingUrl = "http:" + sWorkingUrl;
else
sWorkingUrl = "http://" + sWorkingUrl;
} // End if
// Check for invalid parameters
if (!sUrl.IsEmpty())
{
// URL is not empty
/* Check the URL - must be valid and of the 'http:'
service type */
DWORD dwServiceType;
CString sServer, sObject;
unsigned short nPort;
if (AfxParseURL(sWorkingUrl,
dwServiceType,
sServer,
sObject,
nPort))
{
//URL is valid. Now check service type.
if (dwServiceType == AFX_INET_SERVICE_HTTP)
{
//Service type is valid. Make connection.
pSession = new CInternetSession(
m_sUserAgent,
1,
INTERNET_OPEN_TYPE_PRECONFIG);
pConnection = pSession->GetHttpConnection(sServer,
nPort,
NULL,
NULL);
pHttpFile = pConnection->OpenRequest(
CHttpConnection::HTTP_VERB_HEAD,
sObject,
NULL,
1,
NULL,
NULL,
INTERNET_FLAG_EXISTING_CONNECT
| INTERNET_FLAG_RELOAD
| INTERNET_FLAG_DONT_CACHE);
if (pHttpFile->SendRequest()) /* SendRequest worked */
{
DWORD dwHttpStatus;
// Check the http return code
if(!pHttpFile->QueryInfoStatusCode(dwHttpStatus))
dwHttpStatus = 400;
if (dwHttpStatus >= 400)
{
switch(dwHttpStatus)
{
case 404:
nResult = CROBOT_ERR_NOT_FOUND;
break;
case 403:
case 407:
nResult = CROBOT_ERR_NOT_AUTHORIZED;
break;
default:
nResult = CROBOT_ERR_CONNECTION_FAILED;
break;
} // End switch
unsigned long nFlag = 1; // HTTP_QUERY_CUSTOM;
unsigned long nChars = 1023;
DWORD nFlag2 = HTTP_QUERY_RAW_HEADERS_CRLF;
pHttpFile->QueryInfo(nFlag2, sResult, NULL);
sResponse = sResult;
} // End if dwHttpStatus
else /* No error - read response data */
{
nResult = CROBOT_ERR_SUCCESS;
// Read the data
do
{
nRead = pHttpFile->Read(pBuffer, 1023);
if (nRead != 0)
{
pBuffer[nRead] = 0;
sResult += pBuffer;
} // End if
} while (nRead != 0);
sResponse = sResult;
} // End else
} // End if pHttpFile
else /* SendRequest failed */
nResult = CROBOT_ERR_CONNECTION_FAILED;
} // End if
else
// Wrong service
nResult = CROBOT_ERR_INVALID_URL;
} // End if
else
// Invalid URL
nResult = CROBOT_ERR_INVALID_URL;
} // End if
else
// Empty URL
nResult = CROBOT_ERR_INVALID_PARAMETER;
} // End try
catch (CInternetException* e)
{
e->Delete();
sResponse = sResult;
// Internet exception occurred
nResult = CROBOT_ERR_CONNECTION_FAILED;
} // End catch
catch (...)
{
sResponse = sResult;
// Exception occurred
nResult = CROBOT_ERR_CONNECTION_FAILED;
} // End catch
// Clean up and exit function
if (pBuffer != NULL)
{
delete pBuffer;
pBuffer = NULL;
} // End if
if (pHttpFile != NULL)
{
pHttpFile->Close();
delete pHttpFile;
} // End if
if (pConnection != NULL)
{
pConnection->Close();
delete pConnection;
} // End if
if (pSession != NULL)
{
pSession->Close();
delete pSession;
} // End if
sErrMsg = ErrorMessage(nResult);
if (nResult == CROBOT_ERR_SUCCESS)
return true;
else
return false;
}
// --------------------------------------------------------------
// ********************** public
// * *
// * httpHeaderFields *
// * *
// **********************
// Function: Retrieves a server header for a URL and returns it in
// CString form. Like httpHeader, but also parses header
// into individual field names and values.
//
// Inputs: sURL - The target URL
// (example: "www.mysite.com")
//
// Outputs: <function_result> - True if header was successfully
// retrieved, false otherwise
// sResponse - The header returned by the server
// nResult - Completion code. 0 = success,
// n = error (defined in CRobot.h)
// sErrMsg - The error message, if nResult != 0
// m_sHeader - Copy of the header string
// m_nHeaderFields - Set to number of header fields
// m_sHeadName[] - Contains individual header field names
// m_sHeadValue[] - Contains individual header field values
BOOL CRobotInternet::httpHeaderFields(const CString& sUrl,
CString& sResponse,
int& nResult,
CString& sErrMsg)
{
CInternetSession* pSession;
CHttpConnection* pConnection;
CHttpFile* pHttpFile;
CString sHeader;
int nRead;
LPSTR pBuffer;
CString sResult;
CString sWorkingUrl;
CString sMsg;
CString sHTML, sTemp;
int nPos;
sErrMsg = "";
nResult = CROBOT_ERR_SUCCESS;
m_nHeaderFields = 0;
try
{
pSession = NULL;
pConnection = NULL;
pHttpFile = NULL;
sHeader = CreateStandardHeader();
nRead = 0;
pBuffer = new char[1024];
sResult = "";
sWorkingUrl = sUrl;
/* Trim URL and add http:// if it contains no
protocol identifier */
sWorkingUrl.TrimLeft();
sWorkingUrl.TrimRight();
if (sWorkingUrl.Find(":") == -1)
{
if (sWorkingUrl.Left(1) == "/")
sWorkingUrl = "http:" + sWorkingUrl;
else
sWorkingUrl = "http://" + sWorkingUrl;
} // End if
// Check for invalid parameters
if (!sUrl.IsEmpty())
{
// URL is not empty
/* Check the URL - must be valid and of the 'http:'
service type */
DWORD dwServiceType;
CString sServer, sObject;
unsigned short nPort;
if (AfxParseURL(sWorkingUrl,
dwServiceType,
sServer,
sObject,
nPort))
{
// URL is valid. Now check service type.
if (dwServiceType == AFX_INET_SERVICE_HTTP)
{
//Service type is valid. Make connection.
pSession = new CInternetSession(
m_sUserAgent,
1,
INTERNET_OPEN_TYPE_PRECONFIG);
pConnection = pSession->GetHttpConnection(sServer,
nPort,
NULL,
NULL);
pHttpFile = pConnection->OpenRequest(
CHttpConnection::HTTP_VERB_HEAD,
sObject,
NULL,
1,
NULL,
NULL,
INTERNET_FLAG_EXISTING_CONNECT
| INTERNET_FLAG_RELOAD
| INTERNET_FLAG_DONT_CACHE);
pHttpFile->SendRequest();
unsigned long nFlag = 1; // HTTP_QUERY_CUSTOM;
unsigned long nChars = 1023;
DWORD nFlag2 = HTTP_QUERY_RAW_HEADERS_CRLF;
pHttpFile->QueryInfo(nFlag2, sResult, NULL);
sResponse = sResult;
nResult = CROBOT_ERR_SUCCESS;
} // End if
else
// Wrong service
nResult = CROBOT_ERR_INVALID_URL;
} // End if
else
// Invalid URL
nResult = CROBOT_ERR_INVALID_URL;
} // End if
else
// Empty URL
nResult = CROBOT_ERR_INVALID_PARAMETER;
} // End try
catch (CInternetException* e)
{
e->Delete();
sResponse = sResult;
// Internet exception occurred
nResult = CROBOT_ERR_CONNECTION_FAILED;
} // End catch
catch (...)
{
sResponse = sResult;
// Exception occurred
nResult = CROBOT_ERR_CONNECTION_FAILED;
} // End catch
// Clean up and exit function
if (pBuffer != NULL)
{
delete pBuffer;
pBuffer = NULL;
} // End if
if (pHttpFile != NULL)
{
pHttpFile->Close();
delete pHttpFile;
} // End if
if (pConnection != NULL)
{
pConnection->Close();
delete pConnection;
} // End if
if (pSession != NULL)
{
pSession->Close();
delete pSession;
} // End if
sErrMsg = ErrorMessage(nResult);
if (nResult == CROBOT_ERR_SUCCESS)
{
// Parse header into individual fields
m_sHeader = sResponse;
m_nHeaderFields = 0;
sHTML = sResponse;
if (sHTML.Right(1) != "\n")
sHTML += "\n";
nPos = sHTML.Find("\n");
while (nPos != -1)
{
sTemp = sHTML.Left(nPos);
sHTML = sHTML.Mid(nPos + 1);
nPos = sTemp.Find(":");
if (nPos != -1)
{
m_sHeadName[m_nHeaderFields] = sTemp.Left(nPos);
m_sHeadValue[m_nHeaderFields] = sTemp.Mid(nPos+1);
m_sHeadValue[m_nHeaderFields].TrimLeft();
m_sHeadValue[m_nHeaderFields].TrimRight();
m_nHeaderFields++;
} // End if
nPos = sHTML.Find("\n");
} // End while
return true;
} // End if
else
return false;
}
// --------------------------------------------------------------
// ************************ public
// * *
// * httpGetHeaderField *
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -