HTTP POST to Imageshack

Posted by Brandon Schlenker on Stack Overflow See other posts from Stack Overflow or by Brandon Schlenker
Published on 2009-05-31T02:06:49Z Indexed on 2011/01/04 1:53 UTC
Read the original article Hit count: 610

Filed under:
|
|

I am currently uploading images to my server via HTTP POST. Everything works fine using the code below.

NSString *UDID = md5([UIDevice currentDevice].uniqueIdentifier);
NSString *filename = [NSString stringWithFormat:@"%@-%@", UDID, [NSDate date]];
NSString *urlString = @"http://taptation.com/stationary_data/index.php";
request= [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:imageData]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);

However, when I try to convert this to work with Image Shacks XML API, it doesn't return anything. The directions from ImageShack are below.

Send the following variables via POST to imageshack. us /index.php

fileupload; (the image) xml = "yes"; (specifies the return of XML) cookie; (registration code, optional)

Does anyone know where I should go from here?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about cocoa-touch