1

我试图通过我的 iOS 应用程序提交给 Reddit。我可以很好地登录并被发回我保存的 modhash 和 cookie NSUserDefaults

问题是当我发布数据时,我不断收到“USER_REQUIRED”作为响应,即使我在帖子中包含了 modhash 并设置了我的会话 cookie。我什至在我的应用程序委托中包含以下内容:

[[NSHTTPCookieStorage sharedHTTPCookieStorage]
    setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

但它仍然不起作用。这是我的代码:

-(void) post {

    NSString *modhash2 = [[NSUserDefaults standardUserDefaults]  
        objectForKey:@"modhash"];

    NSString *urlString = [NSString 
        stringWithFormat:@"https://ssl.reddit.com/api/submit"];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded;"];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    [request addValue:redditCookie forHTTPHeaderField:@"Cookie"];


    [request setHTTPMethod:@"POST"];
    NSString *httpBody = [NSString stringWithFormat
        :@" ?uh=%@&kind=link&url=%@&sr=%@&title=%@&r=%@&api_type=json",
        modhash2,
        @"www.google.com",
        @"test",
        @"Google.com",
        @"test"];


    [request setHTTPBody:[httpBody dataUsingEncoding:NSASCIIStringEncoding]];

    NSURLResponse* response;
    NSError* error = nil;
    NSData* result = [NSURLConnection
        sendSynchronousRequest:request
        returningResponse:&response
        error:&error];

    NSDictionary *json = [NSJSONSerialization
        JSONObjectWithData:result
        options:NSJSONReadingMutableContainers
        error:nil];

    NSDictionary *responseJSON = [json valueForKey:@"json"];
    NSLog(@"RETURN: %@",responseJSON);
}

有任何想法吗?

4

0 回答 0