Frage

Dieser Code-Schnipsel funktioniert nicht, ich erhalte eine „Authentifizierung fehlgeschlagen.“ Antwort vom Server. Irgendwelche Ideen?

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
                                    initWithURL:
                                    [NSURL URLWithString:@"http://www.tumblr.com/api/write"]];
    [request setHTTPMethod:@"POST"];
    [request addValue:_tumblrLogin forHTTPHeaderField:@"email"];
    [request addValue:_tumblrPassword forHTTPHeaderField:@"password"];
    [request addValue:@"regular" forHTTPHeaderField:@"type"];
    [request addValue:@"theTitle" forHTTPHeaderField:@"title"];
    [request addValue:@"theBody" forHTTPHeaderField:@"body"];

    NSLog(@"Tumblr Login:%@\nTumblr Password:%@", _tumblrLogin, _tumblrPassword);

    [NSURLConnection connectionWithRequest:request delegate:self];

    [request release];

Sowohl _tumblrLogin und _tumblrPassword durch stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding an anderer Stelle in meinem Code ausführen. Mein Login E-Mail ist von der Form „address+test@test.com“. Es funktioniert gut für die Anmeldung direkt auf tumblr, aber ich frage mich, ob das Zeichen „+“ wird Probleme mit der Codierung verursacht? Es ist nicht entgangen ist. Sollte es sein?


Dank Martin Vorschlag, verwende ich jetzt CFURLCreateStringByAddingPercentEscapes mein Login und Passwort zu entkommen. Ich bin immer noch das gleiche Problem haben, aber meine Authentifizierung versagt.

War es hilfreich?

Lösung

Das Problem ist, dass Sie nicht eine richtige HTTP POST-Anforderung erstellen. Eine POST-Anforderung erfordert eine korrekt formatierte mehrteiliger MIME-kodierte Körper alle die Parameter enthält, die Sie an den Server gesendet werden soll. Sie versuchen, die Parameter wie HTTP-Header, die nicht Arbeit überhaupt zu setzen.

Dieser Code wird tun, was Sie wollen, beachten Sie vor allem die NSString Kategorien, die eine gültige Multipart MIME-Zeichenfolge zu erstellen:

@interface NSString (MIMEAdditions)
+ (NSString*)MIMEBoundary;
+ (NSString*)multipartMIMEStringWithDictionary:(NSDictionary*)dict;
@end

@implementation NSString (MIMEAdditions)
//this returns a unique boundary which is used in constructing the multipart MIME body of the POST request
+ (NSString*)MIMEBoundary
{
    static NSString* MIMEBoundary = nil;
    if(!MIMEBoundary)
        MIMEBoundary = [[NSString alloc] initWithFormat:@"----_=_YourAppNameNoSpaces_%@_=_----",[[NSProcessInfo processInfo] globallyUniqueString]];
    return MIMEBoundary;
}
//this create a correctly structured multipart MIME body for the POST request from a dictionary
+ (NSString*)multipartMIMEStringWithDictionary:(NSDictionary*)dict 
{
    NSMutableString* result = [NSMutableString string];
    for (NSString* key in dict)
    {
        [result appendFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n%@\r\n",[NSString MIMEBoundary],key,[dict objectForKey:key]];
    }
    [result appendFormat:@"\r\n--%@--\r\n",[NSString MIMEBoundary]];
    return result;
}
@end


@implementation YourObject
- (void)postToTumblr
{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
                                    initWithURL:
                                    [NSURL URLWithString:@"http://www.tumblr.com/api/write"]];
    [request setHTTPMethod:@"POST"];
    //tell the server to expect 8-bit encoded content as we're sending UTF-8 data, 
    //and UTF-8 is an 8-bit encoding
    [request addValue:@"8bit" forHTTPHeaderField:@"Content-Transfer-Encoding"];
    //set the content-type header to multipart MIME
    [request addValue: [NSString stringWithFormat:@"multipart/form-data; boundary=%@",[NSString MIMEBoundary]] forHTTPHeaderField: @"Content-Type"];

    //create a dictionary for all the fields you want to send in the POST request
    NSDictionary* postData = [NSDictionary dictionaryWithObjectsAndKeys:
                                 _tumblrLogin, @"email",
                                 _tumblrPassword, @"password",
                                 @"regular", @"type",
                                 @"theTitle", @"title",
                                 @"theBody", @"body",
                                 nil];
    //set the body of the POST request to the multipart MIME encoded dictionary
    [request setHTTPBody: [[NSString multipartMIMEStringWithDictionary: postData] dataUsingEncoding: NSUTF8StringEncoding]];
    NSLog(@"Tumblr Login:%@\nTumblr Password:%@", _tumblrLogin, _tumblrPassword);
    [NSURLConnection connectionWithRequest:request delegate:self];
    [request release];
}
@end

Andere Tipps

Gemäß den Antworten auf diese Frage hat, stringByAddingPercentEscapesUsingEncoding: keine vollständige Escape-Codierung durchführen. Aus irgendeinem Grund hat der Corefoundation-Version dieser Methode ist jedoch:

[(NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, 
    (CFStringRef)[[self mutableCopy] autorelease], NULL, 
    CFSTR("=,!$&'()*+;@?\n\"<>#\t :/"), kCFStringEncodingUTF8) autorelease];

Sie können auch NSMutableString der replaceOccurencesOfString:withString:options: Methode verwenden, um den Ersatz manuell zu tun, aber das Verfahren ist repetitiv und ausführlich. ( Siehe hier .)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top