static bool Http_GP(bool bIsHttps,
CString szGetPost,//GET POST
CString szHost , CString szPath,
CString szData , CString szCookie,
int nPort,
bool bHeader, //要头部吗
CString &strContent, CString &strErrMsg
)
{
DWORD m_Flags = 0;
CString sz_Referer;
if (bIsHttps)
{
// m_Flags = INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_NO_COOKIES|
m_Flags = INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|
INTERNET_FLAG_SECURE| //启用ssl模式
INTERNET_FLAG_IGNORE_CERT_CN_INVALID| //忽略ssl模式下的证书名称错误
INTERNET_FLAG_IGNORE_CERT_DATE_INVALID;
sz_Referer = "https://";
}else{
// m_Flags = INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_NO_COOKIES;
m_Flags = INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE;
sz_Referer = "http://";
}
CString m_Header = Ini_szHeaders;
if (Ini_bReferer)
{
sz_Referer = sz_Referer + szHost;
m_Header += "Referer: "+ sz_Referer +"/\r\n";
}
if (Ini_bX_Forwarded_For)
{
m_Header += "X-Forwarded-For: "+ RandIP() +"\r\n";
}
if (szCookie.GetLength() >10)
{
m_Header += "Cookie: ";
m_Header += szCookie+"\r\n";
}
//////////////////////////////////////////////////////////////////////////
HINTERNET m_hInternet;
HINTERNET m_hSession;
HINTERNET m_hRequest;
try {
m_hInternet = InternetOpen(Ini_szUser_Agent, INTERNET_OPEN_TYPE_PRECONFIG , NULL, NULL, 0);
if (!m_hInternet) {
strErrMsg = "Cannot open internet";//m_lastErrorCode = GetLastError();
return false;
}
m_hSession = InternetConnect(m_hInternet, szHost, nPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0,0);
if (!m_hSession) {
strErrMsg = "Cannot connect to internet";//m_lastErrorCode = GetLastError();
if (m_hInternet) {
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
return false;
}
m_hRequest = HttpOpenRequest(m_hSession, szGetPost, szPath, NULL, "", NULL, m_Flags, 0);
if (!m_hRequest) {
strErrMsg = "Cannot perform http request";//m_lastErrorCode = GetLastError();
if (m_hInternet) {
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
if (m_hSession) {
InternetCloseHandle(m_hSession);
m_hSession = NULL;
}
return false;
}
}catch(...) {
strErrMsg = "Memory Exception occured";//m_lastErrorCode = GetLastError();
return false;
}
//////////////////////////////////////////////////////////////////////////
try {
if (bIsHttps)
{
DWORD dwFlags;
DWORD dwBuffLen = sizeof(dwFlags);
InternetQueryOption (m_hRequest, INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&dwFlags, &dwBuffLen);
dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
InternetSetOption (m_hRequest, INTERNET_OPTION_SECURITY_FLAGS, &dwFlags, sizeof (dwFlags) );
}
BOOL BSendRequest = FALSE;
if (szGetPost == "GET")
{
BSendRequest = HttpSendRequest(m_hRequest, m_Header, m_Header.GetLength(), NULL, 0);
}else{
BSendRequest = HttpSendRequest(m_hRequest, m_Header, m_Header.GetLength(), szData.GetBuffer(0), szData.GetLength());
}
if ( BSendRequest )
{
//return true;
}else{
switch( GetLastError() )
{
case ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED:
{
//if (!SetClientCert()) {
strErrMsg = "Cannot perform http request, client authentication needed but couldnt detect required client certificate";
//m_lastErrorCode = GetLastError();
if (m_hInternet) {
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
if (m_hSession) {
InternetCloseHandle(m_hSession);
m_hSession = NULL;
}
if (m_hRequest) {
InternetCloseHandle(m_hRequest);
m_hRequest = NULL;
}
return false;
//}
break;
}
case ERROR_INTERNET_INVALID_CA:
{
strErrMsg = "Cannot perform http request, client authentication needed, invalid client certificate is used";
//m_lastErrorCode = GetLastError();
if (m_hInternet) {
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
if (m_hSession) {
InternetCloseHandle(m_hSession);
m_hSession = NULL;
}
if (m_hRequest) {
InternetCloseHandle(m_hRequest);
m_hRequest = NULL;
}
return false;
break;
}
default:
{
strErrMsg = "Cannot perform http request";
//m_lastErrorCode = GetLastError();
if (m_hInternet) {
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
if (m_hSession) {
InternetCloseHandle(m_hSession);
m_hSession = NULL;
}
if (m_hRequest) {
InternetCloseHandle(m_hRequest);
m_hRequest = NULL;
}
return false;
break;
}
}
}
}catch(...) {
strErrMsg = "Memory Exception occured"; //m_lastErrorCode = GetLastError();
if (m_hInternet) {
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
if (m_hSession) {
InternetCloseHandle(m_hSession);
m_hSession = NULL;
}
if (m_hRequest) {
InternetCloseHandle(m_hRequest);
m_hRequest = NULL;
}
return false;
}
//////////////////////////////////////////////////////////////////////////
//strContent = "";
char buffer[1024];
memset(buffer, 0, 1024);
if (bHeader)
{
DWORD nLength = 1024;//这一句很重要,,因为HttpQueryInfo返回的时候,会改变此值
HttpQueryInfo(m_hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, buffer,(unsigned long *)&nLength, NULL);
strContent.Format("%s", buffer);
memset(buffer, 0, 1024);
}
//////////////////////////////////////////////////////////////////////////
DWORD dwNumberOfBytesRead;
BOOL result;
while(true)
{
result = InternetReadFile(m_hRequest, buffer, 1023, &dwNumberOfBytesRead);
if (dwNumberOfBytesRead == 0 || !result)
{
break;
}
//////////////////////////////////////////////////////////////////////////
for (int i_1=0; i_1<=1023; i_1++)
{
if (buffer[i_1] == '\0')
{
buffer[i_1] = ' ';
}
}
//////////////////////////////////////////////////////////////////////////
buffer[dwNumberOfBytesRead] = '\0';
strContent += buffer;
memset(buffer, 0, 1024);
}
//////////////////////////////////////////////////////////////////////////
if (m_hInternet) {
InternetCloseHandle(m_hInternet);
m_hInternet = NULL;
}
if (m_hSession) {
InternetCloseHandle(m_hSession);
m_hSession = NULL;
}
if (m_hRequest) {
InternetCloseHandle(m_hRequest);
m_hRequest = NULL;
}
return true;
}
从别的开源工程里扣来的,用来GET,POST网络数据,写了一个多线程程序中调用这个,当线程设置为几十个线程时,过一会程序就崩溃了,这个崩溃的时间不确定,也没发现规律,求大牛分析下这段代码有没什么问题
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)