URL encoding to handle __

Source: Internet
Author: User
1, in mobile development, we will encounter a Web site in the browser can be loaded, but on the app can not load the situation or URL resolution does not come out. This kind of situation mostly is because the website contains the special vulgar character, including the Chinese character to cause. There are a number of ways to deal with this, too, online everywhere. However, with the upgrade and update of the app and other systems, some methods are not enough to support the replacement of the programming language. Today, I have summed up this, dare not say later can use, but at present is absolutely first-class.

2, we first cite a URL. are as follows:

@ "https://www.goufree.com/wap/tmpl/product_list.html?keyword= Computer &type= Yellow Flower"
this URL above. We don't see what's wrong with it, right, put it in the browser it can load smoothly, but it can't be loaded in app. Why is this happening? , and how to solve it.

3. To deal with the above problems.

first: The URL containing Chinese characters.

#pragma mark Chinese character transcoding processing-(nsstring*) encodecharactersstring: (nsstring*) inputstring{//** get system number Nsinteger Versonvalue =
    [Uidevice Currentdevice].systemversion.floatvalue; if (versonvalue<7.0) {//** is encoded inputstring = [InputString stringbyaddingpercentescapesusingencoding:n
    Sutf8stringencoding];
        }else{//** We perform Chinese character processing nsmutablestring * outstring = [nsmutablestring stringwithcapacity:0];
            for (unsigned int i=0; i<inputstring.length; i++) {int utfcode = 0;
            void * buffer = &utfCode;
            Nsrange range = Nsmakerange (i, 1); BOOL encode = [inputstring getbytes:buffer maxlength:10 usedlength:null encoding:nsutf16littleendianstringencoding
            Options:nsstringencodingconversionexternalrepresentation Range:range Remainingrange:null]; if (Encode && (utfcode>=0x4e00 && utfcode<=0x9fa5)) {[Outstring appendstring:[input
String Substringwithrange:range]];            }//** Setting inputstring = [InputString Stringbyaddingpercentencodingwithallowedcharac Ters:[nscharacterset charactersetwithcharactersinstring:[nsstring stringwithformat:@ "%@", OutString]].invertedSet
    ];
return inputstring;
 }
we tested the above methods:

    NSString * loadstring = @ "https://www.goufree.com/wap/tmpl/product_list.html?keyword= Computer &word= yellow flower";
    NSLog (@ "%@", [self encodecharactersstring:loadstring]);
    * * output: 2017-01-13 14:09:48.697 URL encoding processing [2537:513951] https://www.goufree.com/wap/tmpl/product_list.html?keyword=% E7%94%b5%e8%84%91&word=%e9%bb%84%e8%8a%b1

Second: URL code containing special characters.

There are two ways of encoding that contain special characters:

1 "The first:

#pragma Mark Special transcoding-(NSString *) encodetopercentescapestring: (NSString *) inputstring{//** get system version Nsinteger vers
    Onvalue = [Uidevice currentdevice].systemversion.floatvalue; if (versonvalue>=8.0) {//** We have to do some processing, processing is to process the rear parameter if (![ InputString containsstring:@ "?"])
        {return inputstring; }else{//** System 8, the following processing method if ([InputString componentsseparatedbystring:@ "?"].
        Count==1) {return inputstring;
    }//** here Nsarray * Divisionarray = [inputstring componentsseparatedbystring:@ "?"];
    * * Processing of the half part of the Web site nsmutablestring * lasturlstring = [nsmutablestring stringwithcapacity:0]; if (divisionarray.count>=2) {for (unsigned int i =1; i<divisionarray.count; i++) {[Lasturlstri
        ng appendformat:@ "%@", [NSString stringwithformat:@ "%@", Divisionarray[i]]];
    }//** Domain name NSString * domainstring = divisionarray.firstobject; * * Encoding processing NsstrinG *encodingstring = (NSString *) cfbridgingrelease (Cfurlcreatestringbyaddingpercentescapes (KCFAllocatorDefault, C FSTRINGREF) Lasturlstring,null, (cfstringref) @ "!@#$^%*+,:; '
    \ "' <> () []{}/\\|", kCFStringEncodingUTF8));
    NSString * outencodingstring =[nsstring stringwithformat:@ "%@%@", domainstring,encodingstring];
    * * Free memory Domainstring=nil,encodingstring=nil,divisionarray=nil;
return outencodingstring;
 }

2 "The second type:

#pragma Mark Special transcoding-(nsstring*) specialencoding: (nsstring*) inputstring{//** get System version Nsinteger Versonvalue = [Uidevi
    CE currentdevice].systemversion.floatvalue; if (versonvalue>=8.0) {//** We have to do some processing, processing is to process the rear parameter if (![ InputString containsstring:@ "?"])
        {return inputstring; }else{//** System 8, the following processing method if ([InputString componentsseparatedbystring:@ "?"].
        Count==1) {return inputstring;
    }//** here Nsarray * Divisionarray = [inputstring componentsseparatedbystring:@ "?"];
    * * Processing of the half part of the Web site nsmutablestring * lasturlstring = [nsmutablestring stringwithcapacity:0]; if (divisionarray.count>=2) {for (unsigned int i =1; i<divisionarray.count; i++) {[Lasturlstri
        ng appendformat:@ "%@", [NSString stringwithformat:@ "%@", Divisionarray[i]]];
    }//** Domain name NSString * domainstring = divisionarray.firstobject; * * Set the encoding condition NSString * charactersetstring = @ "!@#$^%*+,:; '
    \ "' <> () []{}/\\|];"
    * * Type conversion, prevent warning nsstring * encodingstring = [NSString stringwithformat:@ "%@", lasturlstring]; * * to encode encodingstring = [encodingstring stringbyaddingpercentencodingwithallowedcharacters:[nscharacterset
    Charactersetwithcharactersinstring:charactersetstring].invertedset];
    * * Request URL inputstring = [nsstring stringwithformat:@ "%@?%@", domainstring,encodingstring];
    * * Free memory divisionarray=nil,domainstring = Nil,encodingstring=nil;
return inputstring;
 }

we tested and compared the two methods of appeal:

    * * Contains special characters of the URL
    nsstring * specialstring = @ "https://www.goufree.com/wap/tmpl/product_list.html?keyword={}";
    * * We observe specialstring, we find "{}", a special characters character. If we force the load is not successful.
    NSLog (@ "%@", [self specialencoding:specialstring]);
    * * output: 2017-01-13 14:27:15.899 URL encoding processing [2817:591189] Https://www.goufree.com/wap/tmpl/product_list.html??keyword=%7B%7D
    * * Contains special characters of the URL
    nsstring * specialstring = @ "https://www.goufree.com/wap/tmpl/product_list.html?keyword={}";
    * * We observe specialstring, we find "{}", a special characters character. If we force the load is not successful.
    NSLog (@ "%@", [self encodetopercentescapestring:specialstring]);
    * * output: 2017-01-13 14:27:15.899 URL encoding processing [2817:591189] Https://www.goufree.com/wap/tmpl/product_list.html??keyword=%7B%7D


3, we encoded the URL to decode

#pragma mark Restore page encoding
-(nsstring*) reductionencoding: (nsstring*) inputstring{
    inputstring = [inputstring Stringbyremovingpercentencoding];
    return inputstring;
}
Testing of methods:

    * * Decoding Test
    NSString * testing = @ "Https://www.goufree.com/wap/tmpl/product_list.html??keyword=%7B%7D";
    NSLog (@ "%@", [self reductionencoding:test]);
    * * output: 2017-01-13 14:38:07.183 URL encoding processing [2998:653386] https://www.goufree.com/wap/tmpl/product_list.html??keyword={}
4, the proposal.

There are a lot of coding URLs on the Web page, but there are very few available, and it is recommended that you test them.

5, the method of obtaining demo

This blog is accompanied by a corresponding demo, if there is a need to add qq:1542100658 or QQ Group: successful QQ bar. Replies are available.









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.