Search code examples
iosiphoneobjective-cios7nsmutableurlrequest

Run two urls with NSMutableURLRequest


i've working on the below code to send image to web server. its working fine. i need to run two urls at time with single NSMutableURLRequest.

  NSString *requestString =[NSString stringWithFormat:@"UserId=%@&CategoryId=%@&Continent=%@&Country=%@&City=%@&Gender=%@&ImageName=%@",PassedUserId,CategoryId,continentTextfield.text,countrytextfield.text,citytextfield.text,GenderText.text,imagename];
NSLog(@"%@",requestString);

NSString *url=[NSString stringWithFormat:@"http://192.168.2.4:98/UserImage.svc/InsertObjectImage?%@",requestString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"POST"];

// Create 'POST' MutableRequest with Data and Other Image Attachment.

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
NSData *data = UIImageJPEGRepresentation(chosenImage2, 0.1f);
[request addValue:@"image/JPEG" forHTTPHeaderField:@"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:data]];
[request setHTTPBody:body];

NSData *returnData;
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(@"Ret: %@",returnString);

Solution

  • You said

    I need to run two urls at time with single NSMutableURLRequest.

    As far as I know that's not possible.

    When you submit an URL request, you do it with an NSURLConnection or NSURLSession. Those can manage each request separately and asynchronously.

    Just create 2 requests and submit them separately. You then need to keep track of the responses separately, but it's pretty easy to do so.