IOS request URL return content character set encoding handling adaptive
The beautiful Life of the Sun Vulcan (http://blog.csdn.net/opengl_es)
This article follows "Attribution-non-commercial use-consistent" authoring public agreement
Reprint Please keep this sentence: Sun Vulcan's Beautiful Life-this blog focuses on Agile development and mobile and IoT device research: IOS, Android, HTML5, Arduino, Pcduino , Otherwise, the article from this blog refused to reprint or re-reproduced, thank you for your cooperation.
Let's take a look at how to access the Web service, without considering security link issues for the time being. In this example, I want to access my blog's RSS feeds in a secure way.
Lets first look at how to access our "Web service" without the security overhead. In this example I want to access the RSS feed for my blog in a secure fashion.
//Our secure service:-)Nsurl *Server= [NsurlURLWithString:@"http://www.cocoanetics.com/feed/"];nsurlrequest *Request= [nsurlrequestRequestwithurl:Server];//Use synchronous convenience methodNsurlresponse *Response= Nil;Nserror *Error= Nil;NSData *Returneddata= [nsurlconnectionSendsynchronousrequest:Requestreturningresponse:&Amp;responseerror:&Amp;error];if (!Returneddata){NSLog(@"Error Retrieving data,%@",[Error localizeddescription]);return NO;}//Get the correct text encoding//Http://stackoverflow.com/questions/1409537/nsdata-to-nsstring-converstion-problemCfstringencoding cfencoding=Cfstringconvertianacharsetnametoencoding((Cfstringref)[Response Textencodingname]); Nsstringencoding encoding=Cfstringconvertencodingtonsstringencoding(Cfencoding);//OutputNSString *Xml= [[[NSStringAlloc]Initwithdata:Returneddata encoding:Encoding]Autorelease]; NSLog(@"%@"Xml); |
Here's a pretty trick, not using hard-coded UTF8, we get the appropriate encoding from the answer
We get the XML of my website RSS through this. There is another nifty trick in this, instead of hard coding UTF8 we actually get the appropriate encoding straight from T He response. This is a good the habbit so should adopt the.
IOS automatically handles response content character encoding for Http requests