简体   繁体   English

ios - 编码url字符串后的问题

[英]ios - problems after encoding a url string

I have some code to send a url to a remote server. 我有一些代码将URL发送到远程服务器。 If I do not encode the url, it works perfectly. 如果我不对网址进行编码,它就能完美运行。 But if I encode the url, it does not work. 但是,如果我编码网址,它不起作用。 So I am pretty sure something is not right with the way I encode the url query string. 所以我很确定我编码url查询字符串的方式不对。

Here is my code: 这是我的代码:

        // URL TO BE SUBMITTED.
        NSString *urlString = 
        @"http://www.mydomain.com/test.php?";

        // NOW CREATE URL QUERY STRING    
        NSString *unencoded_query_string = 
        @"name=%@&user_id=%@&person_name=%@&person_email=%@&privacy=%@";

// PUT PREVIOUSLY SET VALUES INTO THE QUERY STRING
        NSString *unencoded_url_with_params = 
        [NSString stringWithFormat:unencoded_query_string, business , user_id , name , email , privacy_string]; 

        // ENCODE THE QUERY STRING
        NSString *escapedString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                                                      NULL,
                                                                                      (__bridge CFStringRef)unencoded_url_with_params,
                                                                                      NULL,
                                                                                      (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                      kCFStringEncodingUTF8);

        // NOW APPEND URL TO QUERY STRING
        NSString *full_encoded_url_string = 
        [urlString  stringByAppendingString: escapedString];

and then I send this string to the server, and the server does have the correct request file invoked, but isn't able to read the parameters. 然后我将此字符串发送到服务器,并且服务器确实调用了正确的请求文件,但无法读取参数。

Would anyone know what I doing incorrectly here? 谁会知道我在这里做错了什么? I am using arc by the way. 我顺便使用arc。

Thanks! 谢谢!

I think you probably want to escape each param, not the entire request. 我想你可能想要逃避每个参数,而不是整个请求。 Basically you want to escape ampersands, spaces etc that show up in your get variables. 基本上你想要逃避你的获取变量中出现的&符号,空格等。 Your encoded URL probably looks like this: 您编码的URL可能如下所示:

http://www.mydomain.com/test.php?name%3DPeter%20Willsey%26user_id%3DUSERID%26person_name%3DPeter%20Willsey%26person_email%3Dpeter%40test.com%26privacy%3D1

and it should look like this: 它看起来应该是这样的:

http://www.mydomain.com/test.php?name=Peter%20Willsey&user_id=25&person_name=Peter%20Willsey&person_email=peter%40test.com&privacy=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM