1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #include "publicFunExport.h"
- #include <stdio.h>
- #include <string>
- #if (defined _WIN32 || defined _WIN64)
- #include <winsock.h>
- #pragma comment(lib,"Ws2_32.lib")
- #include "Wininet.h"
- #pragma comment(lib,"Wininet.lib")
- #else
- #endif
- #if (defined _WIN32 || defined _WIN64)
- bool checkHttpActive(const char* httpUrl)
- {
- TCHAR szHostName[128];
- TCHAR szUrlPath[256];
- URL_COMPONENTS crackedURL = { 0 };
- crackedURL.dwStructSize = sizeof(URL_COMPONENTS);
- crackedURL.lpszHostName = szHostName;
- crackedURL.dwHostNameLength = ARRAYSIZE(szHostName);
- crackedURL.lpszUrlPath = szUrlPath;
- crackedURL.dwUrlPathLength = ARRAYSIZE(szUrlPath);
- InternetCrackUrl(httpUrl, (DWORD)strlen(httpUrl), 0, &crackedURL);
- HINTERNET hInternet = InternetOpen("Microsoft InternetExplorer", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
- if (hInternet == NULL)
- return false;
- HINTERNET hHttpSession = InternetConnect(hInternet, crackedURL.lpszHostName, crackedURL.nPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
- if (hHttpSession == NULL)
- {
- InternetCloseHandle(hInternet);
- return false;
- }
- HINTERNET hHttpRequest = HttpOpenRequest(hHttpSession, "GET", crackedURL.lpszUrlPath, NULL, "", NULL, 0, 0);
- if (hHttpRequest == NULL)
- {
- InternetCloseHandle(hHttpSession);
- InternetCloseHandle(hInternet);
- return false;
- }
- /**
- * ��ѯhttp״̬�루��һ�����DZ���ģ�,����HttpSendRequest()����Ҫ����
- */
- DWORD dwRetCode = 0;
- DWORD dwSizeOfRq = sizeof(DWORD);
- if (!HttpSendRequest(hHttpRequest, NULL, 0, NULL, 0) ||
- !HttpQueryInfo(hHttpRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwRetCode, &dwSizeOfRq, NULL)
- || dwRetCode >= 400)
- {
- InternetCloseHandle(hHttpRequest);
- InternetCloseHandle(hHttpSession);
- InternetCloseHandle(hInternet);
- return false;
- }
- InternetCloseHandle(hHttpRequest);
- InternetCloseHandle(hHttpSession);
- return true;
- }
- #else
- bool checkHttpActive(const char* httpUrl)
- {
- true;
- }
- #endif
|