18,356
社区成员
发帖
与我相关
我的任务
分享
char *CWinInetOperate::WebVisit(CString strUrl, CString strVisitType, char *pcSubmitData, CString strSubmitCookies, CString strSubjoinProtocolHeader,
BOOL bNoRedirect, CString strProxyAddress )
{
CStringOperate c_StrOperate;
CString strTempUrl = strUrl;
BOOL bHttps = FALSE;
strTempUrl.MakeLower();
if (strTempUrl.Left(8) == L"https://")
bHttps = TRUE;
CString strUserAgent;
if (strSubjoinProtocolHeader.Find(L"User-Agent: ") != -1)
strUserAgent = c_StrOperate.GetStringMiddle(strSubjoinProtocolHeader, L"User-Agent: ", L"\r\n");
else
strUserAgent = L"Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)";
HINTERNET hInternet;
if (strProxyAddress.IsEmpty())
hInternet = InternetOpen(strUserAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
else
{
if (bHttps)
hInternet = InternetOpen(strUserAgent, INTERNET_OPEN_TYPE_PROXY, strProxyAddress, NULL, 0);
else
hInternet = InternetOpen(strUserAgent, INTERNET_OPEN_TYPE_DIRECT, L"http=" + strProxyAddress, NULL, 0);
}
if (!hInternet)
return NULL;
HINTERNET hInternetConnect;
hInternetConnect = InternetConnect(hInternet, GetDomain(strUrl), GetPort(strUrl), NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (!hInternetConnect)
{
InternetCloseHandle(hInternetConnect);
InternetCloseHandle(hInternet);
return NULL;
}
int nRequestSign = INTERNET_FLAG_RELOAD | INTERNET_COOKIE_THIRD_PARTY;
if (strSubmitCookies.IsEmpty() == FALSE)
nRequestSign |= INTERNET_FLAG_NO_COOKIES;
if (bNoRedirect)
nRequestSign |= INTERNET_FLAG_NO_AUTO_REDIRECT;
if (bHttps)
nRequestSign |= INTERNET_FLAG_SECURE;
else
nRequestSign |= INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS;
PCTSTR rgpszAcceptTypes[] = {L"text/*",NULL};
HINTERNET hHttpRequest = HttpOpenRequest(hInternetConnect, strVisitType, GetPageAddress(strUrl), L"HTTP/1.1", NULL,NULL, nRequestSign, 0);
if (!hHttpRequest)
{
InternetCloseHandle(hInternet);
InternetCloseHandle(hInternetConnect);
InternetCloseHandle(hHttpRequest);
return NULL;
}
if (strSubjoinProtocolHeader.IsEmpty())
strSubjoinProtocolHeader = L"Accept: */*";
else
{
if (strSubjoinProtocolHeader.Find(L"Accept:") == -1)
strSubjoinProtocolHeader = strSubjoinProtocolHeader + L"\r\n" + L"Accept: */*";
}
if (strSubjoinProtocolHeader.Find(L"Referer:") == -1)
strSubjoinProtocolHeader = strSubjoinProtocolHeader + L"\r\n" + L"Referer: " + strUrl;
if (strSubjoinProtocolHeader.Find(L"Accept-Language:") == -1)
strSubjoinProtocolHeader = strSubjoinProtocolHeader +L"\r\n" +L"Accept-Language: zh-cn";
if (strSubjoinProtocolHeader.Find(L"Content-Type:") == -1)
strSubjoinProtocolHeader = strSubjoinProtocolHeader +L"\r\n"+ L"Content-Type: application/x-www-form-urlencoded";
if (strSubmitCookies.IsEmpty() == FALSE)
strSubjoinProtocolHeader = strSubjoinProtocolHeader + L"\r\n" + L"Cookie: " + strSubmitCookies;
CString strTempVisitType = strVisitType;
strTempVisitType.MakeLower();
if (strTempVisitType == L"get")
HttpSendRequest(hHttpRequest, strSubjoinProtocolHeader, strSubjoinProtocolHeader.GetLength(), NULL, 0);
else
{
DWORD dwLength;
if (pcSubmitData == NULL)
dwLength = 0;
else
dwLength = strlen(pcSubmitData) + 1;
HttpSendRequest(hHttpRequest, strSubjoinProtocolHeader, strSubjoinProtocolHeader.GetLength(), pcSubmitData, dwLength);
}
DWORD nCount;
byte *pbtBuf = new byte[1024];
CByteArray btaryWebCode;
memset(pbtBuf, 0, 1024);
BOOL bFlag;
do
{
bFlag = InternetReadFile(hHttpRequest, pbtBuf, 1024, &nCount);
for (DWORD i = 0; i < nCount; i++)
btaryWebCode.Add(pbtBuf[i]);
} while (nCount);
nCount = btaryWebCode.GetCount();
char *pcWebCode = new char[nCount+1];
memset(pcWebCode, 0, nCount + 1);
for (DWORD i = 0; i < nCount; i++)
pcWebCode[i] = btaryWebCode.GetAt(i);
/* CFile file;
file.Open(L"C:\\Users\\Administrator.USER-20150718LB\\Desktop\\1.txt",CFile::modeReadWrite);
file.Write(pcWebCode,nCount);
file.Close();*/
InternetCloseHandle(hInternet);
InternetCloseHandle(hInternetConnect);
InternetCloseHandle(hHttpRequest);
return pcWebCode;
}