views:

7255

answers:

4

I'm trying to parse XML directly from an HTTPS URL, as follows:

NSString *const URL = @"https://some/HTTPS/url";
NSURL* url = [NSURL URLWithString:URL];
NSXMLParser* parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];

I have the following delegate method for the parser:

- (void) parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qualifiedName 
attributes:(NSDictionary *)attributeDict
{
    NSLog(@"Started parsing %@", elementName);
}

This seems to work fine for HTTP URL's, but shows no result for an HTTPS URL.

How could I fix this?

+3  A: 

You should use NSURLConnection to download the XML data and then parse the output rather than using -initWithContentsOfURL:.

NSURLConnection is more robust and also allows you to do asynchronous fetching which you should definitely be doing, -initWithContentsOfURL: will block the main thread.

Rob Keniger
+6  A: 
  1. None of the initWithContentsOfURL:... methods will allow you to answer the authentication message from the https server. So look at NSURLConnection and NSURLDownload which have delegate messages that help you handle authentication.

  2. To learn more about using URL's for communicating with servers read Introduction to the URL Loading System.

  3. As far as parsing HTML with an XML parser, it will only reliably work with XHTML. So if you are creating and parsing your own XHTML files that should work in most cases. But if you are loading any HTML file from the internet then the XML parser will often not be able to parse the file. You may want to look at WebKit for that.

Nathan Kinsinger
-[NSString initWithContentsOfURL:] is not the same thing as -[NSXMLParser initWithContentsOfURL:]. The latter is not deprecated and does not come in encoding:error: and usedEncoding:error: variants.
Peter Hosey
+1  A: 

Also, most HTTPS servers check the User-Agent string and do not play well when no such header value is specified. It definitely helps to have some (valid) User-Agent string in the url request.

Nocturne
A: 

I'm not sure if you're doing iPhone programming or not, but for the record, in the NSXMLParser Class Reference (in the iPhone 3.0 beta 2 SDK), initWithContentsOfURL:(NSURL *)url does NOT appear to be deprecated.

sandover