Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http.h
- #ifndef HTTP_H_INCLUDED
- #define HTTP_H_INCLUDED
- #include "windows.h"
- #include "winhttp.h"
- #include <iostream>
- #include <string>
- class http_connect
- {
- public:
- http_connect();
- ~http_connect();
- void open_session(LPCWSTR);
- void request(LPCWSTR);
- void read_data(void);
- std::string data (void);
- void end_connection (void);
- private:
- DWORD dwSize ;
- DWORD dwDownloaded;
- LPSTR pszOutBuffer;
- BOOL bResults;
- HINTERNET hSession,
- hConnect,
- hRequest;
- std::string data_ ;
- };
- #endif
- http.cpp
- #include "http.h"
- #include <iostream>
- http_connect::http_connect(){
- DWORD dwSize = 0;
- DWORD dwDownloaded = 0;
- BOOL bResults = FALSE;
- HINTERNET hSession = NULL,
- hhttp_connectnect = NULL,
- hRequest = NULL;
- data_="";
- }
- http_connect::~http_connect()
- {
- delete [] pszOutBuffer;
- }
- void http_connect::open_session(LPCWSTR address)
- {
- hSession = WinHttpOpen( L"Yahoo Weather",
- WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
- WINHTTP_NO_PROXY_NAME,
- WINHTTP_NO_PROXY_BYPASS, 0 );
- // Specify an HTTP server.
- if( hSession )
- hConnect = WinHttpConnect( hSession, address,
- INTERNET_DEFAULT_HTTP_PORT, 0 );
- }
- void http_connect::request(LPCWSTR address)
- {
- // Create an HTTP request handle.
- if( hConnect )
- hRequest = WinHttpOpenRequest( hConnect, L"GET", address ,
- NULL, WINHTTP_NO_REFERER,
- WINHTTP_DEFAULT_ACCEPT_TYPES,
- NULL );
- // Send a request.
- if( hRequest )
- bResults = WinHttpSendRequest( hRequest,
- WINHTTP_NO_ADDITIONAL_HEADERS, 0,
- WINHTTP_NO_REQUEST_DATA, 0,
- 0, 0 );
- // End the request.
- if( bResults )
- bResults = WinHttpReceiveResponse( hRequest, NULL );
- }
- void http_connect::read_data(void)
- {
- // Keep checking for data until there is nothing left.
- if( bResults )
- {
- do
- {
- // Check for available data.
- dwSize = 0;
- if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
- printf( "Error %u in WinHttpQueryDataAvailable.\n",
- GetLastError());
- // Allocate space for the buffer.
- pszOutBuffer = new char[dwSize+1];
- if (!pszOutBuffer)
- {
- printf("Out of memory\n");
- dwSize=0;
- }
- else
- {
- // Read the Data.
- ZeroMemory(pszOutBuffer, dwSize+1);
- if (!WinHttpReadData( hRequest, (LPVOID)pszOutBuffer,
- dwSize, &dwDownloaded))
- {
- printf( "Error %u in WinHttpReadData.\n",
- GetLastError());
- }
- else
- data_+=pszOutBuffer;
- }
- }while( dwSize > 0 );
- }
- }
- std::string http_connect::data (void)
- {
- return data_;
- }
- void http_connect::end_connection (void)
- {
- if( hRequest ) WinHttpCloseHandle( hRequest );
- if( hConnect ) WinHttpCloseHandle( hConnect );
- if( hSession ) WinHttpCloseHandle( hSession );
- data_="";
- };
- main.cpp
- #include "http.h"
- #include<iostream>
- #pragma comment (lib,"winhttp.lib")
- using namespace std;
- http_connect c;
- int main()
- {
- c.open_session(L"maps.googleapis.com");
- c.request(L"/maps/api/staticmap?center=-15.800513,-47.91378&zoom=11&size=200x200&sensor=false");
- c.read_data();
- std::cout<<c.data();
- c.end_connection();
- system("pause");
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement