Objective-C-Äquivalent der Curl-Anforderung

Ich versuche, diese Curl-Anforderung in Objective-C zu bearbeiten:

curl -u username:password "http://www.example.com/myapi/getdata"

Ich habe Folgendes implementiert und es wird ein Fehler beim Abrufen von Daten angezeigtDomain=kCFErrorDomainCFNetwork Code=303 mitNSErrorFailingURLKey=http://www.example.com/myapi/getdata:

// Make a call to the API to pull out the categories
NSURL *url = [NSURL URLWithString:@"http://www.example.com/myapi/getdata"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

// Create the username:password string for the request
NSString *loginString = [NSString stringWithFormat:@"%@:%@", API_USERNAME, API_PASSWORD];

// Create the authorisation string from the username password string
NSData *postData = [loginString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

[request setURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

Ich hatte gehofft, dass jemand erkennen könnte, wo ich falsch liege, wenn ich versuche, die Aufforderung zum Einrollen zu manipulieren und mich in die richtige Richtung zu lenken. Gibt es etwas Offensichtliches, das ich verpasse? Die von der API zurückgegebenen Daten haben ein JSON-Format.

Antworten auf die Frage(2)

Ihre Antwort auf die Frage