Parsing XML with HTTPS URLs with NSXMLParser?

I am 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 the HTTP URL, but does not show the result for the HTTPS URL.

How can i fix this?

+4
source share
4 answers
  • None of the initWithContentsOfURL methods: ... will allow you to reply to an authentication message from an https server. So, take a look at NSURLConnection and NSURLDownload , which delegate messages to help you handle authentication.

  • To learn more about using URLs to communicate with servers, read Introduction to URL Loading .

  • As for parsing HTML using an XML parser, it will only work reliably with XHTML. Therefore, if you create and parse your own XHTML files, which should work in most cases. But if you download any HTML file from the Internet, then the XML parser will often not be able to parse the file. You can see WebKit for this.

+8
source

You should use NSURLConnection to load the XML data and then parse the output, and not use -initWithContentsOfURL :.

NSURLConnection is more reliable, and also allows you to perform asynchronous fetching, which you must do, -initWithContentsOfURL: blocks the main thread.

+4
source

In addition, most HTTPS servers check the User-Agent string and do not play well unless this header value is specified. This definitely helps to have some (valid) User-Agent string in the url request.

+1
source

I'm not sure whether you are programming on the iPhone or not, but for writing to the NSXMLParser Reference Reference (in the SDK for iPhone 3.0 beta 2), initWithContentsOfURL: (NSURL *) url does NOT look obsolete.

0
source

All Articles