iOS asihttp请求如何记录表单

时间:2012-05-17 05:47:45

标签: ios forms post asihttprequest

我需要发送一个ASI http请求,一个帖子表单,

当我检查身体的要求时,它似乎是空的, 如何检查身体是什么?,为什么它是空的?

谢谢!

- (void)sendUnsentEntries {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    if (!sendingEntries) {

        sendingEntries = YES;
        Reachability *reachability = [Reachability reachabilityForInternetConnection];

        BOOL errorsSending = NO;
        NSInteger unsentCount = 0;

        if ([Client unsubmittedCount] > 0 && [reachability isReachable]) {            

            NSString *host = [self apiHost];

             for (Client* clientToSend in [Client allUnsubmitted]) {

                 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", host]];

                 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
                 [request setValidatesSecureCertificate:NO];
                 request.requestMethod = @"POST";

                 [request setPostValue:clientToSend.name forKey:@"name"];
                 [request setPostValue:clientToSend.territory forKey:@"territory"];

                 NSLog(@"name:: %@", clientToSend.name);

                 //LOGING THE FOR THE REQUEST
                 NSString *requ = [[[NSString alloc] initWithData:[request postBody] encoding:NSUTF8StringEncoding] autorelease];

                 NSLog(@"request body: %@", requ);

                 [request startSynchronous];


                 NSError *error = [request error];
                 if (!error) {
                     NSString *responseString = [request responseString];

                     NSLog(@"response :::%@",responseString);

                     if ([responseString isEqualToString:@"true"]) {
                         clientToSend.submitted = [NSNumber numberWithBool:YES];
                     }              



                 } else {
                     NSLog(@"error: %@", [error description]);
                 }

                 [[self managedObjectContext] save:nil];

        }

        unsentCount += [Client unsubmittedCount];




        [[UIApplication sharedApplication] setApplicationIconBadgeNumber:unsentCount];

        if (errorsSending && showSendingErrors) {

        }

    }


    sendingEntries = NO;

    [pool release];
}

}

所以我得到了我的 记录:: request body:空响应,

这是记录表单正文的方法吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

在将表单提交给服务器之前,不会构建帖子正文。

您有两种选择:

  1. [request startSynchronous];
  2. 之后记录帖子正文
  3. 在NSLog
  4. 之前调用[request buildPostBody];