0

我正在尝试使用他们的 API 将个人资料图片上传到 Twitter。我在网上找到了一些关于如何格式化 URLRequest 的好例子,但是我似乎无法让它工作。任何帮助,将不胜感激。我从 Twitter API 使用的 URL 是http://twitter.com/account/update_profile_image.json

-(void)uploadImageForAccount:(NSURL *)theURL intUserName:(NSString *)_userName initPassWord:(NSString *)_passWord image:(NSData *)image{
    userName = [_userName retain];
    passWord = [_passWord retain];

    UIApplication *app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = YES;

    requestedData = [[NSMutableData alloc] initWithLength:0];

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL                                                            cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
                                                          timeoutInterval:60];

    [theRequest setHTTPMethod:@"POST"];

    NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
    [theRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];

    // Credentials
    NSString *creds = [NSString stringWithFormat:@"%@:%@", userName, passWord];
    [theRequest addValue:[NSString stringWithFormat:@"Basic %@",[self base64encode:creds]] forHTTPHeaderField:@"Authorization"];

    NSMutableData *postBody = [NSMutableData data];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"source\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"canary"] dataUsingEncoding:NSUTF8StringEncoding]];

    NSString *mimeType = mimeType = @"image/jpeg";

    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media\"; filename=\"%@\"\r\n", @"TNImage.jpeg"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n", mimeType] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Transfer-Encoding: binary\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:image];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [theRequest setHTTPBody:postBody];

    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest 
                                                                  delegate:self 
                                                          startImmediately:YES];

    if(DEBUG) NSLog(@"Connection = %@",[connection description]);
    if (connection == nil) {
        if(DEBUG) NSLog(@"Connection was Nil");

    }
}
4

1 回答 1

-1

Json 不起作用,而是使用 xml

于 2010-01-17T21:03:14.447 回答