Hi,
I'm retrieving an XML file from a HTTP server using a CFReadStreamRef. However, I'm having some troubles determining whether there's a valid connection or not.
I've got the following bit of code (all in a separate thread):
ReadStream = CFReadStreamCreateForHTTPRequest( kCFAllocatorDefault, Request); if( !ReadStream) ThrowRuntimeError( "CFReadStreamCreateForHTTPRequest failed");
if( !CFReadStreamOpen( ReadStream)) ThrowRuntimeError( "CFReadStreamOpen failed");
UInt32 StatusCode = 0; CFHTTPMessageRef ResponseHeader = NULL; while( !ResponseHeader) { ResponseHeader = (CFHTTPMessageRef)CFReadStreamCopyProperty( ReadStream, kCFStreamPropertyHTTPResponseHeader); if( !ResponseHeader) continue;
StatusCode = CFHTTPMessageGetResponseStatusCode( ResponseHeader); } if( ResponseHeader) CFRelease( ResponseHeader);
Works perfectly when there's a valid network connection. However, once you unplug the network the problem arises. Instead of failing to open the read stream (like I expected) it just opens without any error, but it obviously never gets the response headers so it just hangs there.
Is there any way to either detect a network connection, or to determine that the request failed?
Thanks,
Maarten Kools |