我正在尝试实现一个Objective C程序来与activeCollab 3 beta接口,并且我遇到了一些问题 . 我可以输入NSLog在浏览器中输出的URL,它可以很好地拉动所有项目的xml,当我尝试通过这个程序访问它时它不想为我工作,它给了我一个HTTP 403错误 . 我是Objective C的新手,我这样做是为了学习经验,所以有些代码可能是多余的 . 在此先感谢您的帮助 . 导入包含在尖括号中,但会导致它隐藏在StackOverflow上,所以我将它放在引号中
#import "Foundation/Foundation.h"
int main (int argc, const char * argv[]) {
NSString *token = @"my-token";
NSString *path_info = @"projects";
NSString *url = @"http://my-site/api.php?";
NSString *post = [[NSString alloc] initWithFormat:@"path_info=%@&auth_api_token=%@",path_info, token];
NSLog(@"Post: %@", post);
NSString *newRequest;
newRequest = [url stringByAppendingString:post];
NSLog(@"Path: %@", newRequest);
NSData *postData = [newRequest dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
[request setURL:[NSURL URLWithString:newRequest]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *returnData = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"%@", returnData);
//printf("Print line");
return 0;
}
1 回答
您的请求标头限制且不必要,对于AC API,您需要GET请求而不是POST