Creating a class

I'm pretty new to OOP and it's kind of rare for me to use C++, but I think know the basics. I'm using Visual C++ 2010 Express on Windows 8.
What I want to do at the moment, is to create a class to make WinHttp requests easier. This is the code that I'm basically using to build the class:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa384270%28v=vs.85%29.aspx#code-snippet-1

I hope you can give me some tips and suggestions!

This is what I got so far. (Completely untested)
Example of how I hope that it will work:
1
2
3
4
HttpRequest MyRequest;
MyRequest.Initialize("Mozilla/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0","","");
htmlCode = MyRequest.SendRequest("www.cplusplus.com","GET","");
cout << htmlCode << endl;

The code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "stdafx.h"
#include <windows.h>
#include <winhttp.h>
#pragma comment(lib, "winhttp.lib")

class HttpRequest {
  private:
    DWORD dwSize;
    DWORD dwDownloaded;
    LPSTR pszOutBuffer;
    BOOL  bResults;
    HINTERNET hSession;
    HINTERNET hConnect;
    HINTERNET hRequest;
    LPCTSTR userAgent;
    LPCTSTR proxyIp;
    LPCTSTR proxyPort;
    LPCTSTR url;
    LPCTSTR method;
    LPVOID body;
  public:
    void Initialize(char*, char*, char*);
    LPSTR SendRequest(char*, char*, char*);
};

void HttpRequest::Initialize(char* _userAgent, char* _proxyIp, char* _proxyPort) {
    userAgent = (LPCTSTR)_userAgent;
    proxyIp = (LPCTSTR)_proxyIp;
    proxyPort = (LPCTSTR)_proxyPort;
    //I couldn't figured out how I could pass the proxy to the function...
    hSession = WinHttpOpen( userAgent, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0 );
}

LPSTR HttpRequest::SendRequest(char* _url, char* _method, char* _body) {
    url = (LPCTSTR)_url;
    method = (LPCTSTR)_method;
    body = (LPVOID)_body;
    if (body == L"")
        body = WINHTTP_NO_REQUEST_DATA;
    hRequest = WinHttpOpenRequest( hConnect, method, NULL, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0 );
    bResults = WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, body, 0, 0, 0 );
    if( bResults )
        bResults = WinHttpReceiveResponse( hRequest, NULL );
    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 { 
                    printf( "%s", pszOutBuffer );
                    return pszOutBuffer;
                }
                
                // Free the memory allocated to the buffer.
                //delete [] pszOutBuffer;
            }
        } while( dwSize > 0 );
    }
    
    
    // Report any errors.
    if( !bResults )
        printf( "Error %d has occurred.\n", GetLastError( ) );
    
    // Close any open handles.
    if( hRequest ) WinHttpCloseHandle( hRequest );
    if( hConnect ) WinHttpCloseHandle( hConnect );
    if( hSession ) WinHttpCloseHandle( hSession );
}

(this is the first class I have written so far, I hope I didn't completely misunderstand the principle)
Last edited on
A class should have a state, and it should take advantage of constructors and destructors. You're not really using class functionality to its best effect.
Thanks, I will look into these and get back with my result. :)
When first posting I forgot the header part of the class, I added it now.

I googled a bit, but I don't really understand what's the point of using a constructor. Should I simply turn the Initialize() function into the constructor?

And for the destructor, do I really need to declare one myself? As far as I understood this, the compiler will create one itself.
The compiler doesn't know that you want to construct your object from three strings, how could it possibly generate that constructor for you?
Is it possible that you confused my questions?

I was talking about the destructor when I said, that the compiler might be able to take care of it.
I'm not sure what the default destructor does, but I assume that it will delete the object and kind of free the memory.
I was also asking if I should simply turn the Initialize() function into the constructor.

Sorry if I'm confusing things here.
Yes, you can easily turn your initialize function into the constructor, and yes I did confuse your questions.

As for the destructor, if you initialize any of your members by hand you need to free them by hand too in the destructor.
Topic archived. No new replies allowed.