NSURLRequest,为什么 craigslist 返回 404?
我正在构建我的第一个 iPhone 应用程序,但遇到了困难。
我正在尝试构建一个 RSS 阅读器,并且正在尝试使用来自 craigslist 的提要。此代码使用 stackoverflow,返回“状态代码:200”:
- (void)parseRSSFeed:(NSString *)feed withDelegate:(id)delegate
{
responseData = [[NSMutableData data] retain];
feed = @"http://stackoverflow.com";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:feed]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"status code: %d", [((NSHTTPURLResponse*) response) statusCode]);
[responseData setLength:0];
}
一切正常。但是,如果我将 feed 更改为“http://portland.craigslist.org/muc/”,我会收到状态代码 404。
有什么我遗漏的吗? Craigslist 是否禁止 iPhone 访问其网站?我需要对 URL 进行一些转义吗?
带有 craigslist URL 的代码位于此处。这正是我正在使用的,它返回 404:
- (void)parseRSSFeed:(NSString *)feed withDelegate:(id)delegate
{
responseData = [[NSMutableData data] retain];
feed = @"http://portland.craigslist.org/muc/";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:feed]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
I'm building my first iPhone app and I'm stymied.
I'm trying to build an RSS reader, and I'm trying to use a feed from craigslist. This code, using stackoverflow, returns "Status code: 200":
- (void)parseRSSFeed:(NSString *)feed withDelegate:(id)delegate
{
responseData = [[NSMutableData data] retain];
feed = @"http://stackoverflow.com";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:feed]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"status code: %d", [((NSHTTPURLResponse*) response) statusCode]);
[responseData setLength:0];
}
All is well. But if I change feed to, say, "http://portland.craigslist.org/muc/", I get a status code of 404.
Is there anything I'm missing? Does Craigslist disallow iPhone access to its website? Is there some escaping I need to do on the URL?
The code, with the craigslist URL, is here. This is exactly as I'm using it, and it returns a 404:
- (void)parseRSSFeed:(NSString *)feed withDelegate:(id)delegate
{
responseData = [[NSMutableData data] retain];
feed = @"http://portland.craigslist.org/muc/";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:feed]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我想通了。 Craigslist 正在阻止此请求。当您更改应用程序的名称时,相同的代码将起作用。例如,当应用程序名称包含字符串“craig”时,返回的响应代码为 404。当我将应用程序名称更改为不包含名称“craig”时,响应代码为 200。
Ok i figured it out. Craigslist is blocking this request. The same code works when you change the name of the app. For example when the app name contained the string "craig" the response code that was returned was 404. When i changed the app name to not contain the name "craig" then the response is 200.
如果 User-Agent 标头包含字符串“craig”,Craigslist 会阻止 URL 请求。默认情况下,Apple 会将您的应用程序名称包含在 URL 请求的 User-Agent 标头中。要绕过该块,请使用 NSMutableURLRequest 而不是 NSURLRequest 并伪造 User-Agent 标头:
Craigslist blocks URL requests if the User-Agent header contains the string "craig". Apple, by default, includes your app name in the User-Agent header of your URL requests. To get around the block, use NSMutableURLRequest instead of NSURLRequest and fake out the User-Agent header: