NSURLRequest, why does craigslist return a 404?

微笑、不失礼 提交于 2019-12-04 17:13:48

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.

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:

#define kUserAgentString @"Mozilla/5.0 (Windows; U; Windows NT 6.1; tr-TR) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27"

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:theURL] autorelease];
NSString *userAgent = kUserAgentString;
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!