Gee*_*Out 0 nsurlconnection ios ios5
我正在尝试使用此代码进行远程服务器调用:
- (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
}
}];
}
Run Code Online (Sandbox Code Playgroud)
如果我不对URL进行编码,它实际上会返回而不会出现错误.但是,如果我对url进行编码,则会出现此错误:
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"}
Run Code Online (Sandbox Code Playgroud)
这个错误是什么意思?我编码不正确吗?此外,当我不编码它时,数据和响应对象返回值,但我不知道如何从这些对象中获取这些值.如何判断这些对象中的数据?
谢谢!
问题是您正在编码整个字符串,其中包括方案和URI元素.您可能只想对URL的查询部分进行编码.
例如,如果您的URL是http://www.example.com/whatever?u=jason&p=pass.!!,您想要对查询部分进行编码,而不是其他任何内容.此示例的正确编码的URL如下所示:
http://www.example.com/whatever?u=jason&p=pass%2E%21%21
因为你只是编码整个字符串,所以你最终会得到这样的结果:
http%3A%2F%2Fwww%2Eexample%2Ecom%2Fwhatever%3Fu%3Djason%26p%3Dpass%2E%21%21
这不是有效的URL.而不是转义整个字符串,只是逃避您的查询部分.如果您的后端服务器可以处理它,您可以构建和转义整个查询字符串,因为只有您的服务器将解析该部分.如果由于任何原因它无法处理它,你将不得不逃避查询字符串的键和值部分.
| 归档时间: |
|
| 查看次数: |
5109 次 |
| 最近记录: |