NSURLConnection:我正在尝试接收 html 页面,但总是收到错误 403
我想获取 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 错误。当我删除“Accept”标头时,它开始工作。即使没有谷歌开发者密钥,它也可以工作。
I want to get html page and parse it.
I send to 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];
I always get 403 error. What is wrong ?
EDITED:
I tried my request on http://bbc.co.uk/ and it works well. My problem only with http://gdata.youtube.com/feeds/api/videos?q=u_dAYLIG984
EDITED2:
@Christophe Debove, you are right. I changed my request from POST to GET and received 406 error. It became working when I deleted "Accept" header. It works even without google developer key.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来服务器禁止你。当您忘记传递安全参数或者某些标头值错误时,可能会发生这种情况。
我尝试使用 LiveHTTPHeader Firefox 模块发送请求。
我看到youtube api不接受POST请求,只接受GET。
当我使用
POST
方法发送时,我得到403 forbiden Target feed is read-only
因此,请使用
GET
更改方法,您可以删除您的一些headers 的值即使不会导致 pb 也毫无用处。
It seems server forbid you. It can happen when you forget to pass security parameters or you're wrong with some headers values.
I try to send the request with LiveHTTPHeader the firefox module.
And I saw youtube api doesn't accept POST request but only GET.
When I send with
POST
method I get403 forbiden Target feed is read-only
So please change the method with
GET
you can remove some of your headers value even if they don't cause the pb there are useless.