-1

抱歉,这里是初学者的问题。我正在尝试使用 HTTP post 从 URL 中检索 XML 文档,并且我需要在 POST 请求中包含一些参数。我已经从这个网站的 Objective C 部分获得了代码并开始理解它。但是我现在需要尝试做的是在我的应用程序中使用该类。以下是我需要包含的参数:

URL: https://admin.poslavu.com/cp/reqserv/ 
dataname=tangere_techno&key=dBkeY&token=dBt0kEn&table=menu_groups

</p>

4

1 回答 1

4

尝试:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://admin.poslavu.com/cp/reqserv/"]];
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
[request setHTTPBody:[[NSString stringWithFormat:@"dataname=tangere_techno&key=dBkeY&token=dBt0kEn&table=menu_groups"] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];
NSError *error = nil; NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error) {
    NSLog(@"Error:%@", error.localizedDescription);
}
else {
    //success
}

数据应该是您的 XML 信息,但您需要先对其进行解析。您应该尝试查看 Apple 的文档以获取更多信息。如果您尝试异步发出请求,请尝试以下操作:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://admin.poslavu.com/cp/reqserv/"]];
[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
[request setHTTPBody:[[NSString stringWithFormat:@"dataname=tangere_techno&key=dBkeY&token=dBt0kEn&table=menu_groups"] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];


AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:req];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
    NSData *data = (NSData *)responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
[operation start];
于 2013-07-31T16:44:32.150 回答