The below code i am using for sending multiple images along with the text. but only one image is saving in Web server. Here the problem is i need to get response from the first url and i've to assign it to the second url.
NSLog(#"PassedID%#",PassedUserId);
NSLog(#"integer=%#", [[NSUserDefaults standardUserDefaults] objectForKey:#"Person"]);
NSString *CategoryId=#"3";
NSString *imagename=#"ComparisonObject";
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"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *uploadImage1 = [session dataTaskWithRequest:request completionHandler:^(NSData *data2, NSURLResponse *response, NSError *error) {
// Finish uploading image 1
// Get response and data to prepare to update image 2
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField:#"Content-Type"];
NSData *data = UIImageJPEGRepresentation(chosenImage1, 0.2f);
[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(#"recievedData%#",receivedData);
NSString *imagename=#"ComparisonObject";
NSString *requestString1 =[NSString stringWithFormat:#"UserId=%#&ImageId=%#&=ImageName%#",PassedUserId,compareId,imagename];
NSLog(#"%#",requestString1);
NSString *url=[NSString stringWithFormat:#"http://192.168.2.4:98/UserImage.svc/UpdateObjectImage?%#",requestString1];
NSMutableURLRequest *request2 = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:#"POST"];
NSURLSessionDataTask *uploadImage2 = [session dataTaskWithRequest:request2 completionHandler:^(NSData *data1, NSURLResponse *response, NSError *error) {
NSLog(#"recievedData%#",receivedData);
NSString *imagename=#"ComparisonObject";
NSString *requestString1 =[NSString stringWithFormat:#"UserId=%#&ImageId=%#&=ImageName%#",PassedUserId,compareId,imagename];
NSLog(#"%#",requestString1);
NSString *url=[NSString stringWithFormat:#"http://192.168.2.4:98/UserImage.svc/UpdateObjectImage?%#",requestString1];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField:#"Content-Type"];
NSData *data = UIImageJPEGRepresentation(chosenImage2, 0.2f);
[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];
// Finish uploading image 2
}];
}];
Related
I am trying to post data to WCF service from iPhone Application but somehow service doesn't seems to be call through the iPhone. I received a Status Code = 999.
NSString *urlString = #"http://192.168.1.192/MSservice/SecureRegistration";
NSURL *URL = [NSURL URLWithString:
[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *postData = [bodyData stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:#"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setValue:#"gzip" forHTTPHeaderField:#"Accept-Encoding"];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Current-Type"];
[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
NSError *errorReturned = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
if (errorReturned)
{
NSLog(#"Error %#", errorReturned.localizedDescription);
}
else
{
NSLog(#"Data %#", data);
NSString *responseString=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"Response %#", responseString);
}
I have one text field I need to send two data's to API.
NSString *post =[NSString stringWithFormat:#"val1=%#,val2=%#",[[NSUserDefaults standardUserDefaults] objectForKey:#"val1"],val2];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"http://api.test.com/send"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"log%#",str);
}
I need to send only raw data to API this is my code.Please check and let me know thanks
NSData *postData = [someStringToPost dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:someURLString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:[NSString stringWithFormat:#"%d", postData.length] forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSData *retData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
if (error)
{
//error
}
else
{
//no error
}
Try this
Have you tried converting it to NSData and sending that?
Here's the conversion:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
That will give you the data in bytes, then just pass it in with the normal networking function calls.
I am getting the PassedUserId value from another view.I can print the value in log. but its not assigning to the url string. Here i am trying to pass the text along with image.
NSString *requestString =[NSString stringWithFormat:#"UserId=%#&CategoryId=%#&Continent=%#&Country=%#&City=%#&Gender=%#&ImageName=%#&AgeRange=%#",PassedUserId,CategoryId,continentTextfield.text,countrytextfield.text,citytextfield.text,gender,imagename,ageTextfield.text];
NSString *url=[NSString stringWithFormat:#"http://192.168.2.4:98/UserImage.svc/InsertFacialImage?%#",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(chosenImage, 0.2f);
[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);
The first thing I noticed is that you have swapped the two values:
stringWithFormat:#"UserId=%#&CategoryId=%#...",CategoryId,PassedUserId
Could you be more specific about what datatypes the CategoryId and PassedUserId have?
If they're strings, you're doing it right. If they are datatype integer though, you should use %i instead of %#.
Below code i wrote for sending two images. But i failed to find the code why its not working. i am new to iOS and even stackoverflow. Still am thinking the code is ok. just help me to find my mistake. how i modify the code to able to send two images with different services(each service have a separate url).
NSString *requestString =[NSString stringWithFormat:#"requestUserId=%#&CategoryId=%#&Continent=%#&Country=%#&City=%#&Gender=%#&ImageName=%#&AgeRange=%#",userID,CategoryId1,continentTextfield.text,countrytextfield.text,citytextfield.text,GenderText.text,imagename,ageTextfield.text];
NSLog(#"request%#",requestString);
NSString *url=[NSString stringWithFormat:#"http://37.187.152.236/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(chosenImage, 0.2f);
[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);
NSURLConnection *connReq = [NSURLConnection connectionWithRequest:request delegate:self];
if (connReq) {
NSLog(#"Connection Sucessful");
receivedData = [[NSMutableData alloc]init];
}
else {
NSLog(#"failed");
}
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(#"Status code: %d", [response statusCode]);
NSLog(#"respdata%#",respData);
if ([response statusCode]==200)
{
self.userId=[[NSUserDefaults standardUserDefaults]objectForKey:#"Sendd"];
NSLog(#"recievedData%#",receivedData);
NSString *requestString1 =[NSString stringWithFormat:#"requestUserId=%#&ImageId=%#&=ImageName%#",userID,compareId,imagename];
NSLog(#"%#",requestString1);
NSString *url1=[NSString stringWithFormat:#" http://37.187.152.236/UserImage.svc/UpdateObjectImage?%#",requestString1];
NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc] init] ;
[request1 setURL:[NSURL URLWithString:url1]];
[request1 setHTTPMethod:#"POST"];
// Create 'POST' MutableRequest with Data and Other Image Attachment.
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request1 setValue:contentType forHTTPHeaderField:#"Content-Type"];
NSData *data = UIImageJPEGRepresentation(chosenImage, 0.2f);
[request1 addValue:#"image/JPEG" forHTTPHeaderField:#"Content-Type"];
NSMutableData *body1 = [NSMutableData data];
[body1 appendData:[NSData dataWithData:data]];
[request setHTTPBody:body1];
NSData *returnData;
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"Ret: %#",returnString);
NSURLConnection *connReq = [NSURLConnection connectionWithRequest:request delegate:self];
if (connReq) {
NSLog(#"Connection Sucessful");
receivedData = [[NSMutableData alloc]init];
}
else {
NSLog(#"failed");
}
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(#"Status code: %d", [response statusCode]);
NSLog(#"respdata%#",respData);
homeViewController *homVew =[[homeViewController alloc]initWithNibName:#"homeViewController" bundle:nil];
self.navigationItem.hidesBackButton = YES;
[self.navigationController pushViewController:homVew animated:YES];
}
The code make me very happy to Post image into Web server. its working smart for single image. The code what i've to used to Post a single image is
NSLog(#"%#",requestString);
NSString *url=[NSString stringWithFormat:#"http://37.187.152.236/UserImage.svc/InsertFacialImage?%#",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(chosenImage, 0.2f);
[request addValue:#"image/JPEG" forHTTPHeaderField:#"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:data]];
[request setHTTPBody:body];
Then i failed to jump over the hurdle, Failed to Post 2 images with different services.The truth behind my failure is after uploading of the 1st image, server generate response then i've attach the response to second service. i did it properly but failed because i made the connection run 2 times for 2 images.but Web services team asking me that run it in a single connection.The code which i is used, which i need to modify is
Posting 1st image
NSString *url=[NSString stringWithFormat:#"http://37.187.152.236/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(chosenImage1, 0.2f);
[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);
NSURLConnection *connReq = [NSURLConnection connectionWithRequest:request delegate:self];
if (connReq) {
NSLog(#"Connection Sucessful");
receivedData = [[NSMutableData alloc]init];
}
else {
NSLog(#"failed");
}
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(#"Status code: %ld", (long)[response statusCode]);
Generating Response
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.receivedData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(#"%#" , error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSError *e;
jsonDict1 = [NSJSONSerialization JSONObjectWithData:receivedData options: NSJSONReadingMutableContainers error: &e];
compareId = [jsonDict1 valueForKey:#"ImageId"];
NSLog(#"JSONN%#" , jsonDict1);
compareId = [results.firstObject objectForKey:#"ImageId"];
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:self.compareId forKey:#"Sendy"];
[defaults synchronize];
NSLog(#"compareId:%#",compareId);
results = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:&e];
}
}
after this step is response is generating good and i can pass it to the second string. so no problem with the server response.
*Post 2nd Image *
NSString *url1=[NSString stringWithFormat:#"http://37.187.152.236/UserImage.svc/UpdateObjectImage?%#",requestString1];
NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc] init] ;
[request1 setURL:[NSURL URLWithString:url1]];
[request1 setHTTPMethod:#"POST"];
// Create 'POST' MutableRequest with Data and Other Image Attachment.
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request1 setValue:contentType forHTTPHeaderField:#"Content-Type"];
NSData *data = UIImageJPEGRepresentation(chosenImage2, 0.2f);
[request1 addValue:#"image/JPEG" forHTTPHeaderField:#"Content-Type"];
NSMutableData *body1 = [NSMutableData data];
[body1 appendData:[NSData dataWithData:data]];
[request1 setHTTPBody:body1];
NSData *returnData;
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"Ret: %#",returnString);
NSURLConnection *connReq = [NSURLConnection connectionWithRequest:request1 delegate:self];
if (connReq) {
NSLog(#"Connection Sucessful");
receivedData = [[NSMutableData alloc]init];
}
else {
NSLog(#"failed");
}
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *respData = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&response error:&error];
NSLog(#"Status code: %ld", (long)[response statusCode]);