21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The next step is to connect to the server. You do this by calling InternetConnect( ),<br />

which will return a new handle to an object that stores all of the relevant connection<br />

information. The two obvious requirements for this function are the name of the<br />

server to connect to and the port on which to connect. The name of the server may<br />

be specified as either a hostname or a dotted-decimal IP address. You can specify the<br />

port as a number or use the constant INTERNET_DEFAULT_HTTPS_PORT to connect to the<br />

default SSL-enabled HTTP port 443.<br />

HINTERNET hConnection;<br />

LPSTR lpszServerName = "www.amazon.com";<br />

INTERNET_PORT nServerPort = INTERNET_DEFAULT_HTTPS_PORT;<br />

LPSTR lpszUsername = 0;<br />

LPSTR lpszPassword = 0;<br />

DWORD dwService = INTERNET_SERVICE_HTTP;<br />

DWORD dwFlags = 0;<br />

DWORD dwContext = 0;<br />

hConnection = InternetConnect(hInternetSession, lpszServerName, nServerPort,<br />

lpszUsername, lpszPassword, dwService, dwFlags,<br />

dwContext);<br />

The call to InternetConnect( ) actually establishes a connection to the remote server.<br />

If the connection attempt fails for some reason, the return value is NULL, and the error<br />

code can be retrieved via GetLastError( ). Otherwise, the new object handle is<br />

returned. If multiple requests to the same server are necessary, you should use the<br />

same handle, to avoid the overhead of establishing multiple connections.<br />

Once a connection to the server has been established, a request object must be constructed.<br />

This object is a container for various information: the resource that will be<br />

requested, the headers that will be sent, a set of flags that dictate how the request is<br />

to behave, header information returned by the server after the request has been submitted,<br />

and other information. A new request object is constructed by calling<br />

HttpOpenRequest( ).<br />

HINTERNET hRequest;<br />

LPSTR lpszVerb = "GET";<br />

LPSTR lpszObjectName = "/";<br />

LPSTR lpszVersion = "HTTP/1.1";<br />

LPSTR lpszReferer = 0;<br />

LPSTR lpszAcceptTypes = 0;<br />

DWORD dwFlags = INTERNET_FLAG_SECURE |<br />

INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTP |<br />

INTERNET_FLAG_IGNORE_REDIRECT_TO_HTTPS;<br />

DWORD dwContext = 0;<br />

hRequest = HttpOpenRequest(hConnection, lpszVerb, lpszObjectName, lpszVersion,<br />

lpszReferer, lpszAcceptTypes, dwFlags, dwContext);<br />

The lpszVerb argument controls the type of request that will be made, which can be<br />

any valid HTTP request, such as GET or POST. The lpszObjectName argument is the<br />

resource that is to be requested, which is normally the part of a URLthat follows the<br />

Securing Web Communication on Windows Using the WinInet API | 465<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!