0

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

当我检查我的请求的正文时,它似乎是空的,如何检查正文是什么?为什么它是空的?

谢谢!

- (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];
}

}

所以我得到了我的 log of::request body:一个空响应,

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

谢谢!

4

1 回答 1

1

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

你有两个选择:

  1. 之后记录帖子正文[request startSynchronous];
  2. [request buildPostBody];在 NSLog 之前调用
于 2012-05-17T13:16:59.943 回答