2013-11-21 31 views
0

我试图发送一个异步Post请求到一个PHP的URL,但该请求需要一个“密码”的参数和值“EGOT”。我敢肯定,我需要使用这样的:如何使用objective-c中的参数发送异步发布请求?

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

唯一的问题是,当我开始在我的viewController实现文件类型,它不会得到认可。我需要为此导入NSURL吗?我认为UIVIewcontroller已经从类继承。如果这不是得到这个请求的正确方法,请向我解释如何得到它,这将非常感激。

+0

通过参数你的意思是请求头? – Macondo2Seattle

+1

你可以开始使用非常容易使用包装类的[AFNetworking](https://github.com/AFNetworking/AFNetworking)。 –

回答

3

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

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

然后调用NSURLConnection sendAsynchronousRequest:queue:completionHandler:这一请求。

+0

对不起,我只是有点困惑,如何实现sendAsynchronousRequest。在我做了你所说的我之后:[NSURLConnection sendAsynchronousRequest:@“http://ec2-54-243-205-92.compute-1.amazonaws.com/Tests/ping.php”queue:request completionHandler:< #^(NSURLResponse *响应,NSData *数据,NSError * connectionError)处理程序#> {NSGLL(@“working”); }]; } – user3015941

+0

'sendAsynchronousRequest'的第一个参数应该是'NSMutableURLRequest'(就像我的例子),否则就是'NSURLRequest'。这不是一个字符串。查看该方法的文档:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURLConnection/sendAsynchronousRequest :queue:completionHandler: – Macondo2Seattle

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]; 
+0

这是如何回答OP的问题? – Macondo2Seattle

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

这太神奇了! –

相关问题