2013-11-21 37 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

這太神奇了! –

相關問題