Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: CFReadStreamCreateForHTTPRequest



--- At Thu, 30 Mar 2006 11:23:18 -0500, Chris Branch wrote:

>I'm trying to create a simple C++ class that is able to send an HTTP
>request and retrieve the response on OS X (10.2 and later).  For
>example, suppose the interface for my class looks like this:
>
>class cInternetRequest {
>public:
>	cInternetRequest();
>	virtual ~cInternetRequest();
>
>	bool Open(const char *url);
>	bool Read(char *buffer, int bytesToRead, int *bytesRead) const;
>	void Close();
>
>	bool GetHttpStatusCode(int *statusCode) const;
>
>	... more stuff omitted ...
>};
>
>Sample usage of this class might look like:
>
>	if (request.Open("www.somedomain.com/example.php")) {
>		if (request.GetHttpStatusCode(&httpStatus) && httpStatus == 200) {
>			do {
>				success = request.Read(buffer, sizeof(buffer), &numBytes);
>				// ...process response...
>			} while (success && numBytes > 0);
>		}
>
>		request.Close();
>	}
>
>
>It looks like the implementation of this should be pretty
>straightforward using CFHTTPMessageCreateRequest() and
>CFReadStreamCreateForHTTPRequest(), but there are a few aspects that
>Apple's documentation does not seem to cover.
>
>In my Open() method, I would call CFHTTPMessageCreateRequest() to create
>the request, then call CFReadStreamCreateForHTTPRequest() and
>CFStreamOpen() to actually send the request.  Questions:
>
>1) Does CFReadStreamCreateForHTTPRequest() take a "snapshot" of the
>CFHTTPMessageRef parameter?  In other words, is it safe to CFRelease the
>CFHTTPMessageRef immediately after calling
>CFReadStreamCreateForHTTPRequest, or does the CFHTTPMessageRef have to
>persist until the stream is closed/released?
>
>2) Does CFReadStreamOpen() block until HTTP response headers are
>available?  If no, how do I know when it's "safe" to call
>CFReadStreamCopyProperty() to get the response headers?  If
>CFReadStreamOpen() DOES block, what happens if there's a network error? 
>Will it timeout?  How long?

I have implemented something similar (many of us have, I'm sure). 

Looking through the sample code should help to understand the answers to
your questions:

<http://developer.apple.com/samplecode/CFNetworkHTTPDownload/
CFNetworkHTTPDownload.html>

 ...Duane

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/macnetworkprog/email@hidden

This email sent to email@hidden

References: 
 >CFReadStreamCreateForHTTPRequest (From: Chris Branch <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.