0
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:SPACEREQUESTFINAL_URL] cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60];
NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"mymac", @"qerty!98"];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedString]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d",[data length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:data];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:120];
[request  setAllHTTPHeaderFields:jsonDictionary];
NSHTTPURLResponse* urlResponse=nil;
NSError *error=nil;
NSData *serverResponse = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSLog(@"ServerResponse %@",serverResponse);
    if (error==nil)
    {
[NSURLConnection connectionWithRequest:request delegate:self];
NSString *str1=[[NSString alloc]initWithData:serverResponse encoding:NSUTF8StringEncoding];
NSLog(@"SSTR %@",str1);
NSMutableDictionary *JsonDict=[NSJSONSerialization JSONObjectWithData:serverResponse options:NSJSONReadingAllowFragments error:&error];
}

Data is not parsing ServerResponse is coming null -- request is not responding to NSURLConnection may be

Please suggest how to resolve it. Thank you

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
Sri M
  • 1
  • 4
  • Add the `else` clause to `if (error == nil)` and log `[error localizedDescription]` and your knowledge will expand. – trojanfoe Sep 05 '14 at 10:32

1 Answers1

0
 NSMutableDictionary *datadict=[NSMutableDictionary dictionary];
[datadict setObject:[NSString stringWithFormat:@"%@", mymac] forKey:@"yourkeytoidentify"];
[datadict setObject:[NSString stringWithFormat:@"%@",qerty!98] forKey:@"yourkeytoidentify"];
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:datadict options:kNilOptions error:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSURL *someURLSetBefore =[NSURL URLWithString:@" url "];
[request setURL:someURLSetBefore];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:jsonData];
NSError *error;
NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request   returningResponse:&response error:&error];
NSString * string1=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"%@",string1);

}
}
Ganesh Kumar
  • 708
  • 6
  • 25
  • Tried like that but no use.... Data is proper and request(NSMutableURLRequest) also proper. But when I try to sync to data then all the key values are becoming null. – Sri M Sep 05 '14 at 11:37
  • Put break point and check whether you have getting the null values in your string. – Ganesh Kumar Sep 05 '14 at 11:45
  • When i try to print the string it is printing in XML. "The server refused this request because the request entity is in a format not supported by the requested resource for the requested method." – Sri M Sep 05 '14 at 11:53
  • Printing like what? can you put here what you getting in Nslog. – Ganesh Kumar Sep 05 '14 at 11:55
  • sometimes it gives like CFNetwork SSLHandshake failed (-9810) – Sri M Sep 05 '14 at 11:58
  • your printing in xml then why you used [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; – Ganesh Kumar Sep 05 '14 at 12:06
  • try like this NSString * str = [NSString stringWithFormat:@"your Xml format", data]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]]; – Ganesh Kumar Sep 05 '14 at 12:13
  • Am not printing in XML wantedly. I parsed data to JSON trying to access it. But unfortunately its showing as CFNetwork SSLHandshake failed then the result is being printed in XML. – Sri M Sep 05 '14 at 12:14
  • Check this http://stackoverflow.com/questions/19922717/how-to-handle-cfnetwork-sslhandshake-failed-in-ios – Ganesh Kumar Sep 05 '14 at 12:16