Frage

Wie würde ich mich über die in Objective-c-replizierende

curl -u rick@email.com:mypassword http://foo.lighthouseapp.com/projects.xml

Ich habe mit der ASIHTTPRequest Bibliothek herumspielen kann aber scheinen nicht, es herauszufinden,

offensichtlich meine Anfrage zu senden, wie so gibt einen Authentifizierungsfehler:

-(void)submit:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://ldn.lighthouseapp.com/projects/63254-londonist-20/tickets.xml"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request startSynchronous];
    NSError *error = [request error];
    if (!error) {
        NSString *response = [request responseString];
        NSLog(@"response:%@",response);
    } else {
        NSLog(@"error:%@",error);
    }
}

Dies ist wahrscheinlich sehr einfach, ich bin nur fehlt etwas ziemlich großen

War es hilfreich?

Lösung

Von ASIHTTPRequest Dokumentation

Mit einem Request-Header

[request addRequestHeader:@"Authorization"
                    value:[NSString stringWithFormat:@"Basic %@",
                           [ASIHTTPRequest base64forData:
                            [[NSString stringWithFormat:@"%@:%@", theUsername, thePassword]
                             dataUsingEncoding:NSUTF8StringEncoding]]]];

Andere Tipps

Wenn Sie wollen keine Frameworks (wie ich), die Sie in der nächsten Art und Weise tun können:

NSURL *url = [NSURL URLWithString: @"https://api.github.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

NSString *authStr = [NSString stringWithFormat:@"%@:%@", login, password];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];

NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request
         completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
             if (!error) {
              NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
              NSLog(@"%@",responseDictionary);
          }
         }] resume];

Wenn Sie ASIHTTPRequest verwenden, das ist alles, was benötigt wird:

 request.shouldPresentCredentialsBeforeChallenge = YES;
 [request addBasicAuthenticationHeaderWithUsername:@"USER" andPassword:@"PASSWORD"];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top