【发布时间】:2011-11-21 09:34:56
【问题描述】:
我想获取html页面并解析它。
我发送到http://gdata.youtube.com/feeds/api/videos?q=u_dAYLIG984
NSURL* url = [NSURL URLWithString: fullPath];
NSMutableDictionary* headers = [[[NSMutableDictionary alloc] init] autorelease];
[headers setValue:@"application/x-www-form-urlencoded;charset=utf-8" forKey:@"Content-Type"];
[headers setValue:@"text/html" forKey:@"Accept"];
[headers setValue:@"no-cache" forKey:@"Cache-Control"];
[headers setValue:@"no-cache" forKey:@"Pragma"];
[headers setValue:@"close" forKey:@"Connection"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:TIMEOUT_REQUEST];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
conn = [[NSURLConnection alloc] initWithRequest:request delegate:delegate startImmediately:YES];
我总是收到 403 错误。怎么了?
编辑:
我在http://bbc.co.uk/ 上尝试了我的请求,效果很好。我的问题只有http://gdata.youtube.com/feeds/api/videos?q=u_dAYLIG984
编辑2:
@Christophe Debove,你是对的。我将请求从 POST 更改为 GET 并收到 406 错误。当我删除“接受”标题时它开始工作。即使没有谷歌开发者密钥,它也能工作。
【问题讨论】:
-
HTTP 是 HTTP,如果它在浏览器中工作,并且如果您检查您的标头值,无论设备如何,它都必须工作,所以如果使用您的浏览器,您不需要谷歌开发者密钥,它是相同的。请注意,在来自同一 IP 的多个请求之后,如果您不使用 api 方式,一些 webapp 作为谷歌日历会询问您验证码。确保你的管子永远不会问你这个问题。
标签: ios nsurlconnection server-error