IOS SDK Post request using NSURLConnection

6 views
Skip to first unread message

Abin Cheriyan

unread,
Sep 29, 2015, 8:06:29 AM9/29/15
to iPhone SDK Development

Below is my code for sending a post request consisting of 4 base64Image data to my server.

NSString *postURL = [NSString stringWithFormat:@"name=%@&mobile=%@&email=%@&bday=%@&image=%@&img1=%@&img2=%@&img3=%@",@"abin",@123",@"abc@gmail.com",@"01-01-1990",[base64Value]image,[base64 value] of img1,[base64 value] of img2,[base64 value] of img3"];

NSData *postData = [postURL dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:@"MY_URL"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

Here all the image values img1,image,image are base64 values.

While sending this request to the server, 6 out of 10 times, it gets failed.

Over Wi-Fi success rate is high and over Mobile Data it is very low.

Image conversion is done using the below code:

- (NSString *)encodeToBase64String:(UIImage *)image {
NSData *data = UIImageJPEGRepresentation(image, 1.0);
NSData *imageBase64Data = [data base64EncodedDataWithOptions:0];
NSString *imageBase64String = [[NSString alloc] initWithData:imageBase64Data encoding: NSUTF8StringEncoding];
return imageBase64String;
}

Any once please help me with some idea to successfully send multiple images to Server using POST request even when the network connectivity is slow.(2G)


Jesse Tayler

unread,
Sep 29, 2015, 8:08:45 AM9/29/15
to iphonesdkd...@googlegroups.com

On Sep 29, 2015, at 7:58 AM, Abin Cheriyan <abinch...@gmail.com> wrote:

While sending this request to the server, 6 out of 10 times, it gets failed.


what is the error?

Reply all
Reply to author
Forward
0 new messages