如何使用objective -c中的参数发送异步发布请求?

时间:2013-11-21 04:28:20

标签: objective-c nsurlrequest

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

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

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

3 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:1)

使用以下代码进行带有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];
 }];

答案 2 :(得分:0)

您可以使用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];