我有以下代码,它应该简单地加载一个带有发布数据的 URL,然后从服务器获取 HTML 响应:
// Send log in request to server
NSString *post = [NSString stringWithFormat:@"username=%@&password=%@", text_field_menu_username.text, text_field_menu_password.text];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request;
[request setURL:[NSURL URLWithString:@"http://example.com/test.php"]]; // Example Only
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
// Handle response
NSString *response_string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data 1: %@", response);
NSLog(@"Data 2: %@", data);
NSLog(@"Data 3: %@", error);
NSLog(@"Data 4: %@", response_string);
}];
这是每个NSLog
's 的输出:
Data 1: (null)
Data 2: (null)
Data 3: (null)
Data 4:
知道我做错了什么吗?