English 中文(简体)
NSURLRequest, why does craigslist return a 404?
原标题:

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]; 
}
最佳回答

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"];




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签