简体   繁体   English

iOS-编码URL字符串并通过NSURLConnection和NSURLRequest发送给它错误

[英]ios - encoding a url string and sending it via NSURLConnection and NSURLRequest gives error

I am trying to make a remote server call using this code: 我正在尝试使用以下代码进行远程服务器调用:

- (IBAction)login:(id)sender 
{    
    // Arguments are subject and body.
    NSString *urlString = @"my_url";

    NSString *email = self.email.text;
    NSString *password = self.password.text;


    NSString *url_to_send = [NSString stringWithFormat:urlString , email , password];;     

    NSString *escapedString = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                                  (__bridge CFStringRef)url_to_send,
                                                                                  NULL,
                                                                                  (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                                  kCFStringEncodingUTF8);

    // Now send to the server    
    NSURL *url = [NSURL URLWithString:escapedString];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

    // ***************
    // TODO: ok I dont really understand what this is
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    // **************

    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {                  
         NSLog(@"This is data: %@" , data);
         NSLog(@"This is response: %@" , response);
         NSLog(@"This is error: %@" , error);

         if ( error == nil )
         {
             // Display a message to the screen.
         }
         else
         if ([data length] > 0 && error == nil)
         {
                 // Do something
         }
         else 
         {
             // Do something else                         
         }
     }];    
}

if i don't encode the URL, it actually comes back without an error. 如果我不对URL进行编码,则它实际上会返回而没有错误。 But if I encode the url, it gives this error: 但是,如果我对网址进行编码,则会出现此错误:

This is data: (null)
2012-07-08 11:10:56.110 BusinessPlan[1630:14603] This is response: (null)
2012-07-08 11:10:56.111 BusinessPlan[1630:14603] This is error: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x684a180 {NSErrorFailingURLStringKey=My_encoded_url, NSErrorFailingURLKey=my_encoded_url, NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x6895d00 "unsupported URL"}

What does this error mean? 这个错误是什么意思? Did I encode it incorrectly? 我编码不正确吗? Also, when I don't encode it, the data and response objects come back with values, but I wasn't sure how to get those values from those objects. 另外,当我不对其进行编码时,数据和响应对象会返回值,但是我不确定如何从那些对象中获取这些值。 How do I tell what data is in those objectS? 我如何知道这些对象中包含什么数据?

Thanks! 谢谢!

The problem is that you're encoding the entire string, which includes the scheme and the URI elements. 问题是您要对整个字符串进行编码,包括方案和URI元素。 You probably just want to encode the query part of your URL. 您可能只想对URL的查询部分进行编码。

For example, if your URL were http://www.example.com/whatever?u=jason&p=pass.!! 例如,如果您的URL是http://www.example.com/whatever?u=jason&p=pass.!! , you'd want to encode the query part but nothing else. ,您只想对查询部分进行编码,而别无其他。 The properly encoded URL for this example would look like: 此示例的正确编码的URL如下所示:

http://www.example.com/whatever?u=jason&p=pass%2E%21%21

Since you're just encoding the entire string, you're ending up with this: 由于您只是编码整个字符串,因此最终结果如下:

http%3A%2F%2Fwww%2Eexample%2Ecom%2Fwhatever%3Fu%3Djason%26p%3Dpass%2E%21%21

which is not a valid URL. 这不是有效的网址。 Instead of escaping the entire string, just escape your query part. 而不是转义整个字符串,只是转义查询部分。 If your backend server can deal with it, you can build and escape the entire query string because only your server is going to parse that part. 如果您的后端服务器可以处理它,则可以构建和转义整个查询字符串,因为只有服务器将解析该部分。 If it can't deal with it for any reason, you'll have to escape just the key and value parts of the query string. 如果由于某种原因不能处理它,则只需要转义查询字符串的键和值部分。

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

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