Objective C GET方法在WI-FI上工作,但不在蜂窝网络上工作

时间:2012-09-07 16:15:09

标签: objective-c get twitter httprequest

我有以下代码从Twitter API获取推特ID和名称:

{

        NSString *StringUrl = [NSString stringWithFormat:@"https://api.twitter.com/1/users/show.json?screen_name=%@",account.username];

        @try {

                NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
                [request setHTTPMethod:@"GET"];
                [request setTimeoutInterval: 15];
                [request setURL:[NSURL URLWithString:StringUrl]];

                NSError *error = [[NSError alloc] init];
                NSHTTPURLResponse *responseCode = nil;

                NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];

                if([responseCode statusCode] != 200){
                    NSLog(@"Error getting %@, HTTP status code %i", StringUrl, [responseCode statusCode]);
                }else{
                    NSLog(@"Good");
                    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
                    NSString *twitterId = [json objectForKey:@"id_str"];
                    NSString *twitterName = [json objectForKey:@"name"];
                }

        }
        @catch (NSException *exception) {
            NSLog(@"No conection: %@",exception.name);
        }
}

通过wifi网络测试时工作正常;然而,通过蜂窝网络,它回应:

Error getting https://api.twitter.com/1/users/show.json?screen_name=enf_4eva, HTTP status code 400

提前致谢。

1 个答案:

答案 0 :(得分:0)

这似乎不是执行此类请求的最佳方式,因为请求必须通过3G进行身份验证。使用新的SLRequest,我可以设置正在执行请求的帐户,以便它可以正常运行。