Reputation: 13
This is my web service call. if i am calling web services continuously, after some time start getting time out error, but internet is available.
Same web services working properly in android
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
request.timeoutInterval = 30;
request.URL = [NSURL URLWithString:finalURL];
request.HTTPMethod = @"POST";
[request setValue:@"123456" forHTTPHeaderField:@"header"];
if (parameters != nil) {
NSMutableData * body = [[NSMutableData alloc] init];
NSString *boundary = @"---------------------------V2ymHFg03ehbqgZCaKO6jy";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
for (id key in parameters) {
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@", [parameters objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
}
[request addValue:[NSString stringWithFormat:@"%lu", (unsigned long)[body length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:body];
}
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
// This will get the NSURLResponse into NSHTTPURLResponse format
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
if (httpResponse.statusCode == 200) {
NSMutableDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog( @"Response = %@",JSON);
});
} else {
NSLog( @"Error = %@",error);
}
}];
Upvotes: 1
Views: 383
Reputation: 11
It seems like you are accessing wrong array.
for (id key in parameters) { [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@", [parameters objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]]; }
parameters in above code seems to be an string array instead of dictionary.
You can use below code instead.
[parameters enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj,BOOL *_Nonnull stop) { [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@", obj] dataUsingEncoding:NSUTF8StringEncoding]]; }];
Upvotes: 1
Reputation: 584
Try this:
NSString *strServicePath = @"YourURLString";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:strServicePath]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithFormat:@"Boundary-%@", [[NSUUID UUID] UUIDString]];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSDictionary *_params = [NSDictionary dictionaryWithObjectsAndKeys:yourValue,@"yourKey",nil];
// post body
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
for (NSString *param in _params) {
NSMutableString *strPart = [NSMutableString stringWithFormat:@"%@",[self createPartWithKey:param value:[_params objectForKey:param] boundary:boundary encoding:@"8bit" contentType:@"text/plain" contentFilename:@""]];
[body appendData:[strPart dataUsingEncoding:NSUTF8StringEncoding]];
}
//You can give your own strProfileImageName
NSString *strProfileImageName = [NSString stringWithFormat:@"%@_%@_.jpg",@"some_pic",_userID];
if (imageData) {
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=%@\r\n", @"some_pic",strProfileImageName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request queue: [NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
//Your Code Here
}];
Try to implement in this. Hope this helps.
Upvotes: 0