Guest User

Untitled

a guest
Jun 5th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <windows.h>
  5. #include <WinHttp.h>
  6. #include "myHTTP.h"
  7. int main(){
  8. WinHTTP newHTTP("test", "test");
  9. bool myResult;
  10.  
  11. newHTTP.httpConnect(L"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
  12. L"https://pages.awscloud.com/awsomedayonlineconference-reg.html",
  13. 1,
  14. L"GET");
  15.  
  16. newHTTP.httpAddHeader(L"Content-Type: application/x-www-form-urlencodedrn");
  17. newHTTP.httpSend();
  18. myResult = newHTTP.httpReceive();
  19.  
  20.  
  21.  
  22. newHTTP.closeHandles();
  23. return 0;
  24. }
  25.  
  26. hRequest = WinHttpOpenRequest(hConnect, protocol.c_str(), NULL, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
  27.  
  28. if (!hRequest) {
  29. printf("error2: %d", GetLastError());
  30. }
  31.  
  32. error2: 6
  33.  
  34. #pragma once
  35. // WinHTTP wrapper for web protocol -- windows 8.1+
  36. class WinHTTP {
  37.  
  38. private:
  39. std::string siteUsername, sitePassword;
  40. std::wstring UA, URL;
  41. bool bResult = false;
  42. DWORD dwSize = sizeof(DWORD); // used to handle reading data in bytes
  43. LPSTR pszOutBuffer; // used to Allocate space for the buffer.
  44. DWORD dwDownloaded = 0; // set to null if using asynchronously and use in callback function only
  45. HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL;
  46.  
  47. public:
  48. WinHTTP(std::string myuser = "", std::string mypass = "") {
  49. siteUsername = myuser;
  50. sitePassword = mypass;
  51. }
  52.  
  53. // TODO: update to be able to add proxy details either here or before. do check if proxy has been detected in here and open/connect accordingly
  54. void httpConnect(std::wstring userAgent, std::wstring myURL, int isHTTPS, std::wstring protocol) {
  55.  
  56. UA = userAgent;
  57. URL = myURL;
  58.  
  59. std::wstring acceptTypes = L"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
  60.  
  61. int portToUse;
  62. if (isHTTPS == 1) {
  63. portToUse = 443;
  64. }
  65. else {
  66. portToUse = 80;
  67. }
  68.  
  69. //initialize http and return session handle -- use c_str to convert wstring to LPCWSTR
  70. hSession = WinHttpOpen(UA.c_str(),
  71. WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
  72.  
  73. //make the connection request
  74. if (hSession) {
  75. hConnect = WinHttpConnect(hSession, URL.c_str(), portToUse, 0);
  76. }
  77. else {
  78. std::cout << "winhttpconnect error " << GetLastErrorAsString();
  79. }
  80.  
  81. // open the request - not connected at this point
  82. hRequest = WinHttpOpenRequest(hConnect, protocol.c_str(), NULL, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
  83.  
  84. if (!hRequest) {
  85. std::cout << "winhttpopenrequest error " << GetLastErrorAsString();
  86. }
  87.  
  88. }
  89.  
  90. std::string GetLastErrorAsString()
  91. {
  92. //Get the error message, if any.
  93. DWORD errorMessageID = ::GetLastError();
  94. if (errorMessageID == 0)
  95. return std::string(); //No error message has been recorded
  96.  
  97. LPSTR messageBuffer = nullptr;
  98. size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  99. NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
  100.  
  101. std::string message(messageBuffer, size);
  102.  
  103. //Free the buffer.
  104. LocalFree(messageBuffer);
  105.  
  106. return message;
  107. }
  108.  
  109. void httpAddHeader(std::wstring myheader) {
  110.  
  111. if (hRequest) {
  112. bResult = WinHttpAddRequestHeaders(hRequest, myheader.c_str(), (ULONG)-1L, WINHTTP_ADDREQ_FLAG_ADD);
  113. }
  114.  
  115. }
  116.  
  117. bool httpSend() {
  118. if (hRequest) {
  119. bResult = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
  120. }
  121.  
  122. if (!bResult) {
  123. std::cout << "winhttpsendrequest error " << GetLastErrorAsString();
  124. return false;
  125. }
  126. else {
  127. return true;
  128. }
  129. }
  130.  
  131. bool httpReceive() {
  132.  
  133. if (bResult) {
  134. bResult = WinHttpReceiveResponse(hRequest, NULL);
  135. }
  136.  
  137. if (bResult) {
  138.  
  139. do
  140. {
  141. // Check for available data.
  142. dwSize = 0; //query data available looks for data in bytes
  143. if (!WinHttpQueryDataAvailable(hRequest, &dwSize))
  144. {
  145. std::cout << "WinHttpQueryDataAvailable error " << GetLastErrorAsString();
  146. break;
  147. }
  148.  
  149. // No more available data.
  150. if (!dwSize)
  151. return false;
  152.  
  153. // Allocate space for the buffer. as dwSize now holds the size of the request
  154. pszOutBuffer = new char[dwSize + 1]; // just a way of freeing up memory
  155. if (!pszOutBuffer)
  156. {
  157. printf("Out of memoryn"); // couldnt allocate enough
  158. return false;
  159. }
  160.  
  161. ZeroMemory(pszOutBuffer, dwSize + 1); // fills a block of memory with 0s
  162.  
  163. // we know the expect size and have the pszoutbffer to write to - read the Data.
  164. if (!WinHttpReadData(hRequest, (LPVOID)pszOutBuffer,
  165. dwSize, &dwDownloaded))
  166. {
  167. std::cout << "WinHttpReadData error " << GetLastErrorAsString();
  168. return false;
  169. }
  170. else
  171. {
  172. printf("%s", pszOutBuffer);
  173. }
  174.  
  175. // Free the memory allocated to the buffer.
  176. delete[] pszOutBuffer;
  177. return true;
  178.  
  179. // This condition should never be reached since WinHttpQueryDataAvailable
  180. // reported that there are bits to read.
  181. if (!dwDownloaded)
  182. return false;
  183.  
  184. } while (dwSize > 0);
  185.  
  186. }
  187.  
  188. return false;
  189.  
  190. }
  191.  
  192. void closeHandles() {
  193. if (hRequest) WinHttpCloseHandle(hRequest);
  194. if (hConnect) WinHttpCloseHandle(hConnect);
  195. if (hSession) WinHttpCloseHandle(hSession);
  196. }
  197.  
  198. };
  199.  
  200. hSession = WinHttpOpen(UA.c_str(), WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
  201.  
  202. if (hSession) {
  203. hConnect = WinHttpConnect(hSession, URL.c_str(), portToUse, 0);
  204.  
  205. if (hConnect) {
  206. hRequest = WinHttpOpenRequest(hConnect, protocol.c_str(), NULL, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
  207.  
  208. if (!hRequest) {
  209. std::cout << "WinHttpOpenRequest error " << GetLastErrorAsString();
  210. }
  211. }
  212. else {
  213. std::cout << "WinHttpConnect error " << GetLastErrorAsString();
  214. }
  215. }
  216. else {
  217. std::cout << "WinHttpOpen error " << GetLastErrorAsString();
  218. }
  219.  
  220. hRequest = WinHttpOpenRequest(hConnect, protocol.c_str(), path.c_str(), NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
Add Comment
Please, Sign In to add comment