如何将此get方法转换为post?

时间:2016-10-06 09:44:29

标签: ios objective-c web-services

以下是代码。它正在使用GET方法进行解析和请求。

- (void)getStudentsWithOptions:(NSString*)getURLString screen:(NSString *)screenString completion:(SkoolBeepCompletion)completion {

    if (!completion) return;

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:getURLString]];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if (error) {
            completion(nil, error);
        } else {
            NSError *err = nil;
            NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
            //NSLog(@"dataDict : %@", dataDict);
            if (!err) {

                if ([screenString isEqualToString:@"My Students"]) {

                    [CoreDataController deleteAllObjects:@"Students"];

                    NSDictionary *dicEntry = [dataDict objectForKey:@"return_arr"];
                    for (NSDictionary *dicInsert in dicEntry) {

                        [CoreDataController storeStudentsObjects:dicInsert];

                        [CoreDataController storeChildrenObjects:dicInsert];
                    }
                    completion(dataDict, nil);
                } else if ([screenString isEqualToString:@"User Settings"]) {

                    [CoreDataController deleteAllObjects:@"Profile"];

                    [CoreDataController storeProfileObjects:dataDict];

                    completion(dataDict, nil);
                } else if ([screenString isEqualToString:@"About"]) {

                    [CoreDataController deleteAllObjects:@"About"];

                    [CoreDataController storeAboutObjects:dataDict];

                    completion(dataDict, nil);
                } else if ([screenString isEqualToString:@"Works"]) {

                    [CoreDataController deleteAllObjects:@"Works"];

                    [CoreDataController storeWorksObjects:dataDict];

                    completion(dataDict, nil);
                } else {

                    completion(dataDict, nil);
                }
            } else {
                completion(nil, err);
            }
        }
    }];
}

上面是我想在POST中使用的代码行。

URLRequest必须使用POST类型。

我在我的问题中添加了代码。

1 个答案:

答案 0 :(得分:0)

NSMutableURLRequest用于POST

NSString *Post=[NSString stringWithFormat:@"email=%@&password=%@",@"iamiosguy@gmail.com",@"lovetocode"];

NSData *PostData = [Post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *PostLengh=[NSString stringWithFormat:@"%d",[Post length]];
NSURL *Url=[NSURL URLWithString: @"Your URL"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:Url     cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setValue:PostLengh forHTTPHeaderField:@"Content-Lenght"];
[request setHTTPBody:PostData];

NSData *ReturnData =[NSURLConnection sendSynchronousRequest:request returningResponse:Nil error:Nil];

NSString *Response = [[NSString alloc] initWithData:ReturnData encoding:NSUTF8StringEncoding];


NSLog(@"Response%@",Response);