在目标C中使用GET或POST

时间:2014-10-23 06:16:00

标签: objective-c post get

我基本上创建了一个应用程序,我需要验证用户ID和密码。我知道我需要使用POST而不是GET。但我只是乱搞并试图弄清楚东西。

当我在网址中发送两个变量时,会出现我的问题。我的代码是这样的:

NSURL *url = [NSURL URLWithString:@"https://myurl.com?something=whatever&id=%@&pass=%@",Email.text,Password.text];

电子邮件和密码是我的两个文本字段。

我收到错误消息:

  

方法中调用的参数太多,预期为1,有3个

我做错了什么或我该怎么办?

请注意,我正在使用GET。

POST也出现同样的错误。

3 个答案:

答案 0 :(得分:0)

这是在GET请求中执行查询参数的快速而肮脏的方法(您说这仅用于测试?):

NSString *urlStr = [NSString stringWithFormat:@"https://myurl.com?something=whatever&id=%@&pass=%@",
                    [Email.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                    [Password.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSURL *url = [NSURL URLWithString:urlStr];

NSString *responseStr = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:NULL];
NSLog(@"%@", responseStr);

以下是使用POST正确完成的方法:

NSString *url = [NSURL URLWithString:@"https://myurl.com"];
NSString *postStr = [NSString stringWithFormat:"something=whatever&id=%@&pass=%@",
                     [Email.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                     [Password.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[postStr dataUsingEncoding:NSUTF8StringEncoding]];

NSHTTPURLResponse *urlResponse = nil;
NSError *error = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *responseStr = responseData ? [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] : nil;

if (urlResponse.statusCode != 200 || !syncData || !responseStr) {
  // something went wrong...
  NSLog(@"Error processing HTTP %i response: %@", urlResponse.statusCode, error);
  return;
}

// it worked
NSlog(@"%@", rseponseStr);

(注意:这两个都会锁定当前线程。你应该使用dispatch_async()将主线程跳转到后台线程上)

答案 1 :(得分:0)

将此功能用于参数编码

NSData *encodeDictionary(NSDictionary *dictionary)
{
    NSMutableArray *parts = [[NSMutableArray alloc] init];
    for (NSString *key in dictionary) {
        NSString *encodedValue = [[dictionary objectForKey:key] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *encodedKey = [key stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *part = [NSString stringWithFormat: @"%@=%@", encodedKey, encodedValue];
        [parts addObject:part];
    }
    NSString *encodedDictionary = [parts componentsJoinedByString:@"&"];
    return [encodedDictionary dataUsingEncoding:NSUTF8StringEncoding];
}

这里基本上是提出请求的代码。

NSURL *url = [NSURL URLWithString:@"http://localhost/"];
NSDictionary *postDict = @{@"username" : emailField.text, @"password" : passwordField.text};
NSData *postData = encodeDictionary(postDict);

// Create the request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%zd", postData.length] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

// Sending the request
[[NSURLSession sharedSession] dataTaskWithRequest:request
                                completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
 {
     // Decode response and take an action
 }];

答案 2 :(得分:-1)

试试这个:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://myurl.com?something=whatever&id=%@&pass=%@",Email.text,Password.text]];