HTTP POST to Imageshack

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 the Image Shacks XML interface, it returns nothing. Following are directions from ImageShack.

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

FileUpload; (image) xml = "yes"; (indicates XML return) cookie; (registration code optional)

Does anyone know where to go?

0
source share
2 answers

You might want to use ASIHTTPRequest , since it will create the body of the form message for you with much less problems, and may transfer the body of the request from disk, so you save memory when loading large images.

A quick google search of this , which seems to suggest that you should post in / upload _api.php and not in /index.php.

Something like this is likely to be a good start:

 ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithUrl:[NSURL URLWithString:@"http://www.imageshack.us/upload_api.php"]] autorelease]; [request setFile:@"/path/to/file" forKey:@"fileupload"]; [request setPostValue:@"yes" forKey:@"xml"]; [request setPostValue:@"blahblah" forKey:@"cookie"]; //It looks as though you probably need these too [request setPostValue:@" me@somewhere.com " forKey:@"email"]; [request setPostValue:@"blah" forKey:@"key"]; [request start]; if ([request error]) { NSLog(@"%@",[request error]); } else {  NSLog([request responseString]); // The xml that got sent back } 

Warning: unverified!

I used a synchronous request because you did it, but you should almost certainly use an asynchronous request (queue with ASIHTTPRequest).

+2
source

Took me a little, but you need to use ASIHTTPREQUEST!

 - (void)uploadToImageShack { NSAutoreleasePool *paul = [[NSAutoreleasePool alloc] init]; UIImage *tempImage = self.selectedimage; ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.imageshack.us/upload_api.php"]] autorelease]; NSData *imageData = UIImagePNGRepresentation(tempImage); [request setFile:imageData withFileName:@"image" andContentType:@"image/png" forKey:@"fileupload"]; [request setPostValue:@"yes" forKey:@"xml"]; [request setPostValue:@"3ZQ7C09K708fce677d9cadee04811cfcbdf63361" forKey:@"key"]; [request setUseCookiePersistence:NO]; [request startSynchronous]; NSError *error = [request error]; if (error) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; [alert show]; [alert release]; } else if (!error) { if ([request responseString]) { parser = [[NSXMLParser alloc] initWithData:[[NSData alloc] initWithData:[request responseData]]]; [parser setDelegate:self]; [parser parse]; } } [paul drain]; } 
+2
source

Source: https://habr.com/ru/post/946166/