There are special characters in the password how iOS handles issues with Chinese parameters in URLs in iOS

Source: Internet
Author: User

Because the URL supports 26 English letters, numbers, and a few special characters, it is necessary to encode a character that contains a non-standard URL in the URL. The function stringbyaddingpercentescapesusingencoding is provided in iOS to encode Chinese and some special characters, However, the function of stringbyaddingpercentescapesusingencoding is not perfect, which is invalid for some more special characters.

NSString If you include Chinese characters, the conversion to Nsurl gets the value of nil, with stringbyaddingpercentescapesusingencoding:nsutf8stringencoding can be resolved, the code is as follows:

NSString *str = [NSString stringWithUTF8String:surl.c_str ()];
str = @ "http://218.21.213.10/MobileOA/TIFF/45th, about the release of the 2012 city safety of the relative control index of the notice 1.jpg";
str = [STR stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
Nsurl *imageurl = [Nsurl urlwithstring:str];

UIImage *image = [UIImage imagewithdata:[nsdata datawithcontentsofurl:imageurl];

If you do not add str = [str stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; this line is ImageUrl nil

such as requesting a URL request such as "http://m.xxx.com/login.action?username=liyong&psw=&123&time=20120723", The cipher part contains the special character "&123", so the URL is considered to be the symbol of the split parameter, and the parameter is divided into the following error:

username=liyong,psw= empty, 123 = empty, time=20120723

Workaround:

Therefore, if you want to correctly pass a password with a special character, you need to do the following with the password:

Password = (NSString *) cfurlcreatestringbyaddingpercentescapes (NULL, (Cfstringref) Password,null, (cfstringref) @ "!*" ();: @&=+$,/?%#[] ", kCFStringEncodingUTF8);

Note: Such encoding can only be done on the params content, not on the entire URL.

This time the password is "&123", because "&" is translated into "&", so you can pass the password with special characters.

Note: I use the asihttprequest.

We encapsulate it and provide a perfect URL-encoding function.

nsstring* Encodeurl (nsstring* dstring) {nsstring* escapedurlstring= (nsstring*) Cfurlcreatestringbyaddingpercentescapes (null, (CFSTRINGREF) dstring, NULL, (CFSTRINGREF) @ "!*" ();: @&=+$,/?%#[] "   , kCFStringEncodingUTF8); escapedurlstring = [escapedurlstring Stringbyaddingpercentescapesusingencoding:kcfstringencodingutf8];
return [escapedurlstring autorelease]; }

It is important to note that Encodeurl can only be used to encode component in the URL and not to encode the entire URL.

There are special characters in the password how iOS handles issues with Chinese parameters in URLs in iOS

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.