简体   繁体   English

在目标C中使用GET或POST

[英]Using GET or POST in objective C

I'm basically creating an app where I need to verify user ID and password. 我基本上是在创建一个需要验证用户名和密码的应用程序。 I know I need to use POST and not GET. 我知道我需要使用POST而不是GET。 But I'm just messing around and trying to figure out stuff. 但是我只是在四处乱逛,试图找出东西。

MY problem occurs when I'm sending two variables in the URL. 当我在URL中发送两个变量时,发生我的问题。 My code is this : 我的代码是这样的:

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

Email and Password are my two Text fields. 电子邮件和密码是我的两个文本字段。

I get an error that says: 我收到一条错误消息:

Too many arguments called in method, expected 1, have 3 方法中调用的参数过多,期望1,有3

What am I doing wrong or what can I do? 我做错了什么或该怎么办?

Please note, i'm using GET. 请注意,我正在使用GET。

The same error comes for POST. POST也出现相同的错误。

This is a quick and dirty way to do query parameters in a GET request (you said this is just for testing?): 这是在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);

And here's how to do it properly with POST: 这是使用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);

(note: both of these will lock the current thread. You should use dispatch_async() to jump off the main thread onto a background thread) (注意:这两个都将锁定当前线程。您应该使用dispatch_async()将主线程跳到后台线程上)

Use this function for your parameter encoding 使用此功能进行参数编码

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];
}

And here is basically code that will make request. 这基本上是将发出请求的代码。

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
 }];

尝试这个:

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

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

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