【问题标题】:How to send an Asynchronous post request with a parameter in objective -c?如何在objective -c中发送带有参数的异步发布请求?
【发布时间】:2013-11-21 04:28:20
【问题描述】:

我正在尝试向 php url 发送异步 Post 请求,但该请求需要“Password”参数和“EGOT”值。我很确定我需要使用这个:

(void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler

唯一的问题是,当我开始在我的 viewController 实现文件中键入它时,它无法被识别。我需要为此导入 NSURL 吗?我认为 UIVIewcontroller 已经从该类继承。如果这不是获取此请求的正确方法,请向我解释如何获取它,我们将不胜感激。

【问题讨论】:

  • 参数是指请求头?
  • 你可以开始使用AFNetworking,这是一个非常容易使用的包装类。

标签: objective-c nsurlrequest


【解决方案1】:

您可以像这样创建您的请求对象(NSMutableURLRequest 是 'NSURLRequest' 的可变子类):

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request addValue:@"EGOT" forHTTPHeaderField:@"Password"];

然后根据该请求调用NSURLConnection sendAsynchronousRequest:queue:completionHandler:

【讨论】:

【解决方案2】:

将下面的代码用于带有 post 方法的异步请求...

NSString *strupload=[NSString stringWithFormat:@"uid=%@&password=%@&oldpassword=%@",appdel.strUserid,txtConfirmPswd.text,txtOldPswd.text];
NSString *strurl=[NSString stringWithFormat:@"%@change_password.php?",LocalPath];
NSString *strpostlength=[NSString stringWithFormat:@"%d",[strupload length]];
NSMutableURLRequest *urlrequest=[[NSMutableURLRequest alloc]init];

[urlrequest setURL:[NSURL URLWithString:strurl]];
[urlrequest setHTTPMethod:@"POST"];
[urlrequest setValue:strpostlength forHTTPHeaderField:@"Content-Length"];
[urlrequest setHTTPBody:[strupload dataUsingEncoding:NSUTF8StringEncoding]];

[NSURLConnection sendAsynchronousRequest:urlrequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
     NSError *error1;
     NSDictionary *res=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error1];
 }];

【讨论】:

  • 这太棒了!
【解决方案3】:

您可以使用 AFNetworking 进行上传,使用 MBProgressHUD 进行进度显示

AFHTTPClient *httpClient                    = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:urlLink]];
    NSMutableURLRequest *request;

request = [httpClient multipartFormRequestWithMethod:@"POST"
                                                        path:nil
                                                  parameters:postDictionary
                                   constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
                                   }];

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeDeterminate;
    hud.labelText = @"Uploading";

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        float uploadPercentge           = (float)totalBytesWritten / (float)totalBytesExpectedToWrite;
        float uploadActualPercentage    = uploadPercentge * 100;
        hud.progress                    = uploadPercentge;
        if (uploadActualPercentage >= 100) {
            hud.mode        = MBProgressHUDModeText;
            hud.labelText   = [NSString stringWithFormat:@"Waiting for response"];
        }
    }];
    [httpClient enqueueHTTPRequestOperation:operation];

    [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [hud hide:YES];
        NSLog(@"Success %@",  operation.responseString);
        NSDictionary *message = [NSJSONSerialization JSONObjectWithData:[operation.responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil];
        DLog(@"%@",message);
    }
                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                          NSLog(@"error: %@",  operation.responseString);
                                          NSLog(@"%@",error);
                                      }];
    [operation start];

【讨论】:

  • 这如何回答 OP 的问题?
猜你喜欢
  • 2021-06-24
  • 2019-04-28
  • 2014-11-14
  • 2019-10-08
  • 1970-01-01
  • 1970-01-01
  • 2010-10-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多