Advertisement
theman82120

crashes my computer

Nov 10th, 2011
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. http.h
  2.  
  3. #ifndef HTTP_H_INCLUDED
  4. #define HTTP_H_INCLUDED
  5. #include "windows.h"
  6. #include "winhttp.h"
  7. #include <iostream>
  8. #include <string>
  9.  
  10.  
  11. class http_connect
  12. {
  13. public:
  14.     http_connect();
  15.     ~http_connect();
  16.     void open_session(LPCWSTR);
  17.     void request(LPCWSTR);
  18.     void read_data(void);
  19.     std::string  data (void);
  20.     void end_connection (void);
  21. private:
  22.     DWORD dwSize ;
  23.     DWORD dwDownloaded;
  24.     LPSTR pszOutBuffer;
  25.     BOOL  bResults;
  26.     HINTERNET  hSession,
  27.         hConnect,
  28.         hRequest;
  29.     std::string data_  ;
  30. };
  31. #endif
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. http.cpp
  39.  
  40. #include "http.h"
  41.  
  42.  
  43. #include <iostream>
  44.  
  45.  
  46.  
  47. http_connect::http_connect(){
  48.     DWORD dwSize = 0;
  49.     DWORD dwDownloaded = 0;
  50.     BOOL  bResults = FALSE;
  51.     HINTERNET  hSession = NULL,
  52.         hhttp_connectnect = NULL,
  53.         hRequest = NULL;
  54.     data_="";
  55.  
  56.  
  57.  
  58. }
  59.  
  60. http_connect::~http_connect()
  61. {
  62.  
  63.     delete [] pszOutBuffer;
  64.  
  65. }
  66. void http_connect::open_session(LPCWSTR address)
  67. {
  68.     hSession = WinHttpOpen( L"Yahoo Weather",  
  69.         WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
  70.         WINHTTP_NO_PROXY_NAME,
  71.         WINHTTP_NO_PROXY_BYPASS, 0 );
  72.  
  73.  
  74.     // Specify an HTTP server.
  75.     if( hSession )
  76.         hConnect = WinHttpConnect( hSession, address,
  77.         INTERNET_DEFAULT_HTTP_PORT, 0 );
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. void http_connect::request(LPCWSTR address)
  86. {
  87.     // Create an HTTP request handle.
  88.  
  89.     if( hConnect )
  90.         hRequest = WinHttpOpenRequest( hConnect, L"GET", address ,
  91.         NULL, WINHTTP_NO_REFERER,
  92.         WINHTTP_DEFAULT_ACCEPT_TYPES,
  93.         NULL );
  94.  
  95.     // Send a request.
  96.     if( hRequest )
  97.         bResults = WinHttpSendRequest( hRequest,
  98.         WINHTTP_NO_ADDITIONAL_HEADERS, 0,
  99.         WINHTTP_NO_REQUEST_DATA, 0,
  100.         0, 0 );
  101.  
  102.  
  103.     // End the request.
  104.     if( bResults )
  105.         bResults = WinHttpReceiveResponse( hRequest, NULL );
  106. }
  107.  
  108.  
  109.  
  110. void  http_connect::read_data(void)
  111. {
  112.     // Keep checking for data until there is nothing left.
  113.     if( bResults )
  114.     {
  115.  
  116.         do
  117.         {
  118.             // Check for available data.
  119.             dwSize = 0;
  120.             if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
  121.                 printf( "Error %u in WinHttpQueryDataAvailable.\n",
  122.                 GetLastError());
  123.  
  124.             // Allocate space for the buffer.
  125.             pszOutBuffer = new char[dwSize+1];
  126.             if (!pszOutBuffer)
  127.             {
  128.                 printf("Out of memory\n");
  129.                 dwSize=0;
  130.             }
  131.             else
  132.             {
  133.                 // Read the Data.
  134.                 ZeroMemory(pszOutBuffer, dwSize+1);
  135.  
  136.                 if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,
  137.                     dwSize, &dwDownloaded))
  138.                 {
  139.                     printf( "Error %u in WinHttpReadData.\n",
  140.                         GetLastError());
  141.                 }
  142.                 else
  143.                     data_+=pszOutBuffer;
  144.  
  145.  
  146.             }
  147.  
  148.         }while( dwSize > 0 );
  149.     }
  150. }
  151.  
  152. std::string  http_connect::data (void)
  153. {
  154.  
  155.  
  156.     return data_;
  157. }
  158.  
  159.  
  160. void  http_connect::end_connection  (void)
  161. {
  162.     if( hRequest ) WinHttpCloseHandle( hRequest );
  163.     if( hConnect ) WinHttpCloseHandle( hConnect );
  164.     if( hSession ) WinHttpCloseHandle( hSession );
  165.     data_="";
  166.  
  167. };
  168.  
  169. main.cpp
  170.  
  171. #include "http.h"
  172. #include<iostream>
  173. #pragma comment (lib,"winhttp.lib")
  174. using namespace std;
  175. http_connect c;
  176. int main()
  177. {
  178. c.open_session(L"maps.googleapis.com");
  179. c.request(L"/maps/api/staticmap?center=-15.800513,-47.91378&zoom=11&size=200x200&sensor=false");
  180. c.read_data();
  181. std::cout<<c.data();
  182. c.end_connection();
  183. system("pause");
  184.  
  185.  
  186.  
  187.  
  188.  
  189. };
  190.  
  191.  
  192.  
  193.  
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement