Hello guys!

I have found on the twitter-site this faq:

> The image update methods require multipart form data. They do not accept a 
> URL to an image not do they accept the raw image bytes. They instead require 
> the data to be delivered in the form of a file upload filed as defined in 
> RFC1867. The content-type attribute of the image field is checked for valid 
> image type. If you are using PHP/CURL there is a known bug that has since 
> been fixed in the CVS version of PHP. Most installations are not yet using 
> this version and therefore fail during image upload.

I wrote a little code to upload a profile image to my twitter, but
somethings wrong.
Here's my code:

    - (void)uploadProfileImage {
        UIImage *myImage = [UIImage imageNamed:@"pic1.png"];
        NSData *myImageData = [[NSData alloc]
        NSString *bodyString = [NSString stringWithFormat:@"image=%@",
[[[NSString alloc] initWithData:myImageData
encoding:NSUTF8StringEncoding] autorelease]];

        NSString *apiUrl = @"http://twitter.com/account/

        NSURL *url = [NSURL URLWithString:apiUrl];
        NSMutableURLRequest *updateRequest = [NSMutableURLRequest
        [updateRequest setHTTPShouldHandleCookies:NO];
        [updateRequest setHTTPMethod:@"POST"];
        [updateRequest setValue:@"application/x-www-form-urlencoded"
        [updateRequest setHTTPBody:[bodyString
dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
        [updateRequest setValue:[NSString stringWithFormat:@"%d", [bodyString
length]] forHTTPHeaderField:@"Content-Length"];

        NSLog(@"Trying to connect...");
        NSURLConnection *theConnection = [[NSURLConnection alloc]
initWithRequest:updateRequest delegate:self];

        if (theConnection) {
                //_receivedData = [[NSMutableData data] retain];
        } else {
                NSLog(@"Not connected!");
                // Inform the user that the download could not be made

And one more method needed:

    - (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)
challenge {
        if ([challenge previousFailureCount] == 0) {
                NSURLCredential *newCredential = [NSURLCredential
credentialWithUser:@"username" password:@"password"
                [[challenge sender] useCredential:newCredential
        } else {
                [[challenge sender] cancelAuthenticationChallenge:challenge];
                NSLog(@"Invalid username and password!");

And with this method I am receiving the information from twitter:

    - (void)connection:(NSURLConnection *)connection didReceiveData:
(NSData *)data {
        NSLog([[[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding] autorelease]);

But I am getting some strange error. The response isn't a json answer,
it is a html-like answer. Maybe do you have any idea why is that?

Reply via email to