DWORD GetHttpRedirectUrl( LPCTSTR lpSrcUrl, LPTSTR pszDesUrl, DWORD dwSize )
{
CONNECT_INFO info;
ZeroMemory( &info, sizeof(CONNECT_INFO) );
_tcsncpy(pszDesUrl, lpSrcUrl, dwSize);
if ( !ParseURL( lpSrcUrl, &info ) )
return -1;
if ( info.dwServiceType != INTERNET_SERVICE_HTTP )
{
if ( info.dwServiceType== INTERNET_SERVICE_FTP )
return HTTP_STATUS_OK;
return -2;//只支持http和ftp
}
DWORD dwStatusCode = 0;
do
{
TCHAR szAgentName[128] = {0};
_stprintf( szAgentName, _T("KDWS_%x"), this );
// 创建链接
m_hInternet = InternetOpen( szAgentName, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if ( NULL == m_hInternet )
break;
unsigned long lms = 15*1000;
::InternetSetOption(m_hInternet, INTERNET_OPTION_CONTROL_RECEIVE_TIMEOUT, &lms, sizeof(lms) );
m_hSession = InternetConnect(m_hInternet,
info.szHostName,
info.nPort,
_tcslen(info.szUserName) > 0 ? info.szUserName : NULL,
_tcslen(info.szPassword) > 0 ? info.szPassword : NULL,
info.dwServiceType,
0,0 );
if ( NULL == m_hSession)
break;
//初始化下载请求
DWORD dwFlags =
INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_RAW_DATA|INTERNET_FLAG_RELOAD|
INTERNET_FLAG_NO_AUTO_REDIRECT|INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_PRAGMA_NOCACHE;
m_hRequest = HttpOpenRequest(m_hSession, _T("GET"), info.szObject, NULL, _T(""), NULL, dwFlags, NULL);
if ( NULL == m_hRequest )
break;
const TCHAR szHeaders[] = _T("Accept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n");
int ilen = _tcslen(szHeaders);
BOOL bResult = HttpAddRequestHeaders(m_hRequest, szHeaders, ilen, HTTP_ADDREQ_FLAG_ADD);
if ( !bResult )
{
InternetCloseHandle(m_hRequest);
break;
}
if ( HttpSendRequest(m_hRequest, NULL, 0, NULL, 0) )
{
dwStatusCode = GetStatusCode(m_hRequest);
if ( HTTP_STATUS_OK == dwStatusCode )
{
break;
}
if (dwStatusCode == HTTP_STATUS_MOVED ||
dwStatusCode == HTTP_STATUS_REDIRECT ||
dwStatusCode == HTTP_STATUS_REDIRECT_METHOD)
{
//获取跳转地址
TCHAR szBuffer[INTERNET_MAX_URL_LENGTH] = {0};
DWORD dwLen = sizeof(szBuffer);
BOOL bRet = HttpQueryInfo(m_hRequest, HTTP_QUERY_LOCATION,
szBuffer, &dwLen, NULL);
_tcsncpy(pszDesUrl, szBuffer, dwSize);
}
}
} while ( false );
CloseConnect();
return dwStatusCode;
}
{
CONNECT_INFO info;
ZeroMemory( &info, sizeof(CONNECT_INFO) );
_tcsncpy(pszDesUrl, lpSrcUrl, dwSize);
if ( !ParseURL( lpSrcUrl, &info ) )
return -1;
if ( info.dwServiceType != INTERNET_SERVICE_HTTP )
{
if ( info.dwServiceType== INTERNET_SERVICE_FTP )
return HTTP_STATUS_OK;
return -2;//只支持http和ftp
}
DWORD dwStatusCode = 0;
do
{
TCHAR szAgentName[128] = {0};
_stprintf( szAgentName, _T("KDWS_%x"), this );
// 创建链接
m_hInternet = InternetOpen( szAgentName, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if ( NULL == m_hInternet )
break;
unsigned long lms = 15*1000;
::InternetSetOption(m_hInternet, INTERNET_OPTION_CONTROL_RECEIVE_TIMEOUT, &lms, sizeof(lms) );
m_hSession = InternetConnect(m_hInternet,
info.szHostName,
info.nPort,
_tcslen(info.szUserName) > 0 ? info.szUserName : NULL,
_tcslen(info.szPassword) > 0 ? info.szPassword : NULL,
info.dwServiceType,
0,0 );
if ( NULL == m_hSession)
break;
//初始化下载请求
DWORD dwFlags =
INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_RAW_DATA|INTERNET_FLAG_RELOAD|
INTERNET_FLAG_NO_AUTO_REDIRECT|INTERNET_FLAG_DONT_CACHE|INTERNET_FLAG_PRAGMA_NOCACHE;
m_hRequest = HttpOpenRequest(m_hSession, _T("GET"), info.szObject, NULL, _T(""), NULL, dwFlags, NULL);
if ( NULL == m_hRequest )
break;
const TCHAR szHeaders[] = _T("Accept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)\r\n");
int ilen = _tcslen(szHeaders);
BOOL bResult = HttpAddRequestHeaders(m_hRequest, szHeaders, ilen, HTTP_ADDREQ_FLAG_ADD);
if ( !bResult )
{
InternetCloseHandle(m_hRequest);
break;
}
if ( HttpSendRequest(m_hRequest, NULL, 0, NULL, 0) )
{
dwStatusCode = GetStatusCode(m_hRequest);
if ( HTTP_STATUS_OK == dwStatusCode )
{
break;
}
if (dwStatusCode == HTTP_STATUS_MOVED ||
dwStatusCode == HTTP_STATUS_REDIRECT ||
dwStatusCode == HTTP_STATUS_REDIRECT_METHOD)
{
//获取跳转地址
TCHAR szBuffer[INTERNET_MAX_URL_LENGTH] = {0};
DWORD dwLen = sizeof(szBuffer);
BOOL bRet = HttpQueryInfo(m_hRequest, HTTP_QUERY_LOCATION,
szBuffer, &dwLen, NULL);
_tcsncpy(pszDesUrl, szBuffer, dwSize);
}
}
} while ( false );
CloseConnect();
return dwStatusCode;
}