IOS automatically handles response content character encoding for Http requests

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.