NSURLConnection:我正在尝试接收 html 页面,但总是收到错误 403

发布于 2024-12-17 02:54:06 字数 1356 浏览 5 评论 0原文

我想获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若言繁花未落 2024-12-24 02:54:06

看来服务器禁止你。当您忘记传递安全参数或者某些标头值错误时,可能会发生这种情况。

HTTP code 403   Forbidden 

我尝试使用 LiveHTTPHeader Firefox 模块发送请求。
我看到youtube api不接受POST请求,只接受GET。

当我使用 POST 方法发送时,我得到 403 forbiden Target feed is read-only

因此,请使用 GET 更改方法,

[request setHTTPMethod:@"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.

HTTP code 403   Forbidden 

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 get 403 forbiden Target feed is read-only

So please change the method with GET

[request setHTTPMethod:@"GET"];

you can remove some of your headers value even if they don't cause the pb there are useless.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文