21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Solution<br />

The Microsoft WinInet API was introduced with Internet Explorer 3.0. It provides a<br />

set of functions that allow programs easy access to FTP, Gopher, HTTP, and HTTPS<br />

servers. For HTTPS servers, the details of using SSLare hidden from the programmer,<br />

allowing the programmer to concentrate on the data that needs to be<br />

exchanged, rather than protocol details.<br />

Discussion<br />

The Microsoft WinInet API is a rich API that makes client-side interaction with FTP,<br />

Gopher, HTTP, and HTTPS servers easy; as with most Windows APIs, however, a<br />

sizable amount of code is still required. Because of the wealth of options available,<br />

we won’t provide fully working code for a WinInet API wrapper here. Instead, we’ll<br />

discuss the API and provide code samples for the parts of the API that are interesting<br />

from a security standpoint. We encourage you to consult Microsoft’s documentation<br />

on the API to learn about all that the API can do.<br />

If you’re going to establish a connection to a web server using SSLwith WinInet, the<br />

first thing you need to do is create an Internet session by calling InternetOpen( ).<br />

This function initializes and returns an object handle that is needed to actually establish<br />

a connection. It takes care of such details as presenting the user with the dial-in<br />

UI if the user is not connected to the Internet and the system is so configured.<br />

Although any number of calls may be made to InternetOpen( ) by a single application,<br />

it generally needs to be called only once. The handle it returns can be reused<br />

any number of times.<br />

#include <br />

#include <br />

HINTERNET hInternetSession;<br />

LPSTR lpszAgent = "Secure Programming Cookbook Recipe 9.4";<br />

DWORD dwAccessType = INTERNET_OPEN_TYPE_PROXY;<br />

LPSTR lpszProxyName = 0;<br />

LPSTR lpszProxyBypass = 0;<br />

DWORD dwFlags = 0;<br />

hInternetSession = InternetOpen(lpszAgent, dwAccessType, lpszProxyName,<br />

lpszProxyBypass, dwFlags);<br />

If you set dwAccessType to INTERNET_OPEN_TYPE_PROXY, lpszProxyName to 0, and<br />

lpszProxyBypass to 0, the system defaults for HTTP access are used. If the system is<br />

configured to use a proxy, it will be used as required. The lpszAgent argument is<br />

passed to servers as the client’s HTTP agent string. It may be set as any custom<br />

string, or it may be set to the same string a specific browser might send to a web<br />

server when making a request.<br />

464 | Chapter 9: Networking<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!