CFNetwork Programming Guide
Networking & Internet
2009-01-06
Apple Inc.
© 2009 Apple Inc.
All rights reserved.
No part of this publication may be reproduced,
stored in a retrieval system, or transmitted, in
any form or by any means, mechanical,
electronic, photocopying, recording, or
otherwise, without prior written permission of
Apple Inc., with the following exceptions: Any
person is hereby authorized to store
documentation on a single computer for
personal use only and to print copies of
documentation for personal use provided that
the documentation contains Apple’s copyright
notice.
The Apple logo is a trademark of Apple Inc.
Use of the “keyboard” Apple logo
(Option-Shift-K) for commercial purposes
without the prior written consent of Apple may
constitute trademark infringement and unfair
competition in violation of federal and state
laws.
No licenses, express or implied, are granted
with respect to any of the technology described
in this document. Apple retains all intellectual
property rights associated with the technology
described in this document. This document is
intended to assist application developers to
develop applications only for Apple-labeled
computers.
Every effort has been made to ensure that the
information in this document is accurate. Apple
is not responsible for typographical errors.
Apple Inc.
1 Infinite Loop
Cupertino, CA 95014
408-996-1010
Apple, the Apple logo, Bonjour, Cocoa, iChat,
Keychain, Mac, Mac OS, Objective-C, and Safari
are trademarks of Apple Inc., registered in the
United States and other countries.
UNIX is a registered trademark of The Open
Group
Simultaneously published in the United States
and Canada.
Even though Apple has reviewed this document,
APPLE MAKES NO WARRANTY OR REPRESENTATION,
EITHER EXPRESS OR IMPLIED, WITH RESPECT TO
THIS DOCUMENT, ITS QUALITY, ACCURACY,
MERCHANTABILITY, OR FITNESS FOR A PARTICULAR
PURPOSE. AS A RESULT, THIS DOCUMENT IS
PROVIDED “AS IS,” AND YOU, THE READER, ARE
ASSUMING THE ENTIRE RISK AS TO ITS QUALITY
AND ACCURACY.
IN NO EVENT WILL APPLE BE LIABLE FOR DIRECT,
INDIRECT, SPECIAL, INCIDENTAL, OR
CONSEQUENTIAL DAMAGES RESULTING FROM ANY
DEFECT OR INACCURACY IN THIS DOCUMENT, even
if advised of the possibility of such damages.
THE WARRANTY AND REMEDIES SET FORTH ABOVE
ARE EXCLUSIVE AND IN LIEU OF ALL OTHERS, ORAL
OR WRITTEN, EXPRESS OR IMPLIED. No Apple
dealer, agent, or employee is authorized to make
any modification, extension, or addition to this
warranty.
Some states do not allow the exclusion or limitation
of implied warranties or liability for incidental or
consequential damages, so the above limitation or
exclusion may not apply to you. This warranty gives
you specific legal rights, and you may also have
other rights which vary from state to state.
Contents
Introduction
Introduction to CFNetwork Programming Guide 7
Organization of This Document 7
See Also 7
Chapter 1
CFNetwork Concepts 9
When to Use CFNetwork 9
CFNetwork Infrastructure 10
CFSocket API 10
CFStream API 10
CFNetwork API Concepts 11
CFFTP API 12
CFHTTP API 12
CFHTTPAuthentication API 13
CFHost API 13
CFNetServices API 14
CFNetDiagnostics API 14
Chapter 2
Working with Streams 17
Working with Read Streams 17
Working with Write Streams 18
Preventing Blocking When Working with Streams 19
Using Polling to Prevent Blocking 20
Using a Run Loop to Prevent Blocking 21
Navigating Firewalls 23
Chapter 3
Communicating with HTTP Servers 27
Creating a CFHTTP Request 27
Creating a CFHTTP Response 28
Deserializing an Incoming HTTP Request 28
Deserializing an Incoming HTTP Response 29
Using a Read Stream to Serialize and Send HTTP Requests 29
Serializing and Sending an HTTP Request 29
Checking the Response 30
Handling Authentication Errors 30
Handling Redirection Errors 30
Cancelling a Pending Request 31
3
2009-01-06 | © 2009 Apple Inc. All Rights Reserved.
Chapter 4
Communicating with Authenticating HTTP Servers 33
Handling Authentication 33
Keeping Credentials in Memory 37
Keeping Credentials in a Persistent Store 38
Authenticating Firewalls 42
Chapter 5
Working with FTP Servers 43
Downloading a File 43
Setting Up the FTP Streams 43
Implementing the Callback Function 45
Uploading a File 46
Creating a Remote Directory 47
Downloading a Directory Listing 47
Chapter 6
Using Network Diagnostics 49
Document Revision History 51
4
2009-01-06 | © 2009 Apple Inc. All Rights Reserved.
CONTENTS
Figures and Listings
Chapter 1
CFNetwork Concepts 9
Figure 1-1 CFNetwork and other software layers on Mac OS X 9
Figure 1-2 CFStream API structure 11
Figure 1-3 Network diagnostics assistant 15
Chapter 2
Working with Streams 17
Listing 2-1 Creating a read stream from a file 17
Listing 2-2 Opening a read stream 17
Listing 2-3 Reading from a read stream (blocking) 18
Listing 2-4 Releasing a read stream 18
Listing 2-5 Creating, opening, writing to, and releasing a write stream 18
Listing 2-6 Polling a read stream 20
Listing 2-7 Polling a write stream 20
Listing 2-8 Scheduling a stream on a run loop 22
Listing 2-9 Opening a nonblocking read stream 22
Listing 2-10 Network events callback function 22
Listing 2-11 Navigating a stream through a proxy server 23
Listing 2-12 Creating a handle to a dynamic store session 24
Listing 2-13 Adding a dynamic store reference to the run loop 24
Listing 2-14 Loading the proxy dictionary 24
Listing 2-15 Proxy callback function 24
Listing 2-16 Adding proxy information to a stream 25
Listing 2-17 Cleaning up proxy information 25
Chapter 3
Communicating with HTTP Servers 27
Listing 3-1 Creating an HTTP request 27
Listing 3-2 Releasing an HTTP request 28
Listing 3-3 Deserializing a message 28
Listing 3-4 Serializing an HTTP request with a read stream 30
Listing 3-5 Redirecting an HTTP stream 31
Chapter 4
Communicating with Authenticating HTTP Servers 33
Figure 4-1 Handling authentication 34
Figure 4-2 Finding an authentication object 34
Listing 4-1 Creating an authentication object 35
Listing 4-2 Finding a valid authentication object 35
Listing 4-3 Finding credentials (if necessary) and applying them 36
5
2009-01-06 | © 2009 Apple Inc. All Rights Reserved.
评论1