portCheck.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "publicFunExport.h"
  2. #include <stdio.h>
  3. #include <string>
  4. #if (defined _WIN32 || defined _WIN64)
  5. #include <winsock.h>
  6. #pragma comment(lib,"Ws2_32.lib")
  7. #include "Wininet.h"
  8. #pragma comment(lib,"Wininet.lib")
  9. #else
  10. #endif
  11. #if (defined _WIN32 || defined _WIN64)
  12. bool checkHttpActive(const char* httpUrl)
  13. {
  14. TCHAR szHostName[128];
  15. TCHAR szUrlPath[256];
  16. URL_COMPONENTS crackedURL = { 0 };
  17. crackedURL.dwStructSize = sizeof(URL_COMPONENTS);
  18. crackedURL.lpszHostName = szHostName;
  19. crackedURL.dwHostNameLength = ARRAYSIZE(szHostName);
  20. crackedURL.lpszUrlPath = szUrlPath;
  21. crackedURL.dwUrlPathLength = ARRAYSIZE(szUrlPath);
  22. InternetCrackUrl(httpUrl, (DWORD)strlen(httpUrl), 0, &crackedURL);
  23. HINTERNET hInternet = InternetOpen("Microsoft InternetExplorer", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  24. if (hInternet == NULL)
  25. return false;
  26. HINTERNET hHttpSession = InternetConnect(hInternet, crackedURL.lpszHostName, crackedURL.nPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
  27. if (hHttpSession == NULL)
  28. {
  29. InternetCloseHandle(hInternet);
  30. return false;
  31. }
  32. HINTERNET hHttpRequest = HttpOpenRequest(hHttpSession, "GET", crackedURL.lpszUrlPath, NULL, "", NULL, 0, 0);
  33. if (hHttpRequest == NULL)
  34. {
  35. InternetCloseHandle(hHttpSession);
  36. InternetCloseHandle(hInternet);
  37. return false;
  38. }
  39. /**
  40. * ��ѯhttp״̬�루��һ�����DZ���ģ�,����HttpSendRequest()����Ҫ����
  41. */
  42. DWORD dwRetCode = 0;
  43. DWORD dwSizeOfRq = sizeof(DWORD);
  44. if (!HttpSendRequest(hHttpRequest, NULL, 0, NULL, 0) ||
  45. !HttpQueryInfo(hHttpRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwRetCode, &dwSizeOfRq, NULL)
  46. || dwRetCode >= 400)
  47. {
  48. InternetCloseHandle(hHttpRequest);
  49. InternetCloseHandle(hHttpSession);
  50. InternetCloseHandle(hInternet);
  51. return false;
  52. }
  53. InternetCloseHandle(hHttpRequest);
  54. InternetCloseHandle(hHttpSession);
  55. return true;
  56. }
  57. #else
  58. bool checkHttpActive(const char* httpUrl)
  59. {
  60. true;
  61. }
  62. #endif