Before manipulating and obtaining URL properties, you must first create a URL reference by calling the function
URLNewReference
. The URL Access Manager uses a URL reference to uniquely identify a URL and its associated data to be transferred. When you are done with a URL reference, make sure you dispose of the associated memory by calling the function
URLDisposeReference
.
The URL Access Manager provides the functions
URLGetProperty
and
URLSetProperty
to get and set information associated with a URL You must pass the correct data type and size of the property value you wish to set in the propertyBuffer parameter of
URLSetProperty
. Before calling these functions, you should call the function
URLGetPropertySize
to determine the size of the buffer to allocate for the property value.
You may wish to call these functions before calling the functions
URLDownload
and
URLUpload
to get and set information associated with the specified URL in the
urlRef
parameter.
Once you have created a URL reference, you can create a function to display the properties of that reference. In Listing 2-1, the function
displayProperties
first creates a
propertyList
array of Apple-defined URL properties, obtains the corresponding sizes and values of these properties by calling
URLGetPropertySize
and
URLGetProperty
, respectively, and then displays each property value.
Listing 2-1 Displaying the value of each URL property
void displayProperties( URLReference urlRef ) { OSErr err = noErr; int propCount = 0; const char* propertyList[21]; Size propertySize = 0; Handle theProperty = NULL; propertyList[0] = kURLURL; propertyList[1] = kURLResourceSize; propertyList[2] = kURLLastModifiedTime; propertyList[3] = kURLMIMEType; propertyList[4] = kURLFileType; propertyList[5] = kURLFileCreator; propertyList[6] = kURLCharacterSet; propertyList[7] = kURLResourceName; propertyList[8] = kURLHost; propertyList[9] = kURLAuthType; propertyList[10] = kURLUserName; propertyList[11] = kURLPassword; propertyList[12] = kURLStatusString; propertyList[13] = kURLIsSecure; propertyList[14] = kURLCertificate; propertyList[15] = kURLTotalItems; propertyList[16] = kURLHTTPRequestMethod; propertyList[17] = kURLHTTPRequestHeader; propertyList[18] = kURLHTTPRequestBody; propertyList[19] = kURLHTTPRespHeader; propertyList[20] = kURLHTTPUserAgent; // Get the size of each property, allocate a handle to store the // property's value, get the property value, and display it. for( propCount = 0; propCount < 21; propCount++) { // Get the size of the property's value. err = URLGetPropertySize(urlRef, propertyList[propCount], &propertySize); if(err != noErr) printf("Error %d getting property size %s. Size returned was: %d\n", err, propertyList[propCount], propertySize); else printf("Property size is %d: %s\n", propertySize); // Now get a handle for the property value. theProperty = NewHandleClear( propertySize + 1 ); err = MemError(); if(err != noErr) printf("Error %d getting property handle %s\n", err, propertyList[propCount]); else printf("Got handle for %s: %s\n", propertyList[propCount]); // Now get the property's value. err = URLGetProperty(urlRef, propertyList[propCount], *theProperty, propertySize); if(err != noErr) printf("Error %d getting property %s\n", err, propertyList[propCount]); else printf("Property %s: %s\n", propertyList[propCount], *theProperty); // Clean up. DisposeHandle(theProperty); printf("\n"); } return; }