1

I want to create an iPhone application to send data to a Rest API service. If the data is a string defined as geoX#35#geoY#65 which NSS method shall i use. Was thinking of NSString and NSMutablerequest to make my request and define my string but this isn't working atm.Also i am using NSURLconnection to establish a connection with the server(maybe that's faulty too). Anyone that can help ?

Thanks in advance.

1 Answers1

3

Your connection data is using special characters and if you try to do this with GET method of NSURLConnection. It will Shows connection error. For this You have to use POST method like :

NSData *body = nil;
NSString *contentType = @"text/html; charset=utf-8";
NSURL *finalURL = @"YOUR URL WITH the ?input=";
NSString *yourString = @"geoX#35#geoY#65"; 
contentType = @"application/x-www-form-urlencoded; charset=utf-8";
body = [[NSString stringWithFormat:@"%@", yourString] dataUsingEncoding:NSUTF8StringEncoding];
if (nil==finalURL) {
    finalURL = url;
}

NSMutableDictionary* headers = [[[NSMutableDictionary alloc] init] autorelease];
[headers setValue:contentType forKey:@"Content-Type"];
[headers setValue:mimeType forKey:@"Accept"];
[headers setValue:@"no-cache" forKey:@"Cache-Control"];
[headers setValue:@"no-cache" forKey:@"Pragma"];
[headers setValue:@"close" forKey:@"Connection"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:finalURL
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];

[request setAllHTTPHeaderFields:headers];

[request setHTTPBody:body];

self.conn = [NSURLConnection connectionWithRequest:request delegate:self];
Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Sham
  • 475
  • 2
  • 7
  • Thanks a lot! One question where to define url and mimeType? Kinda confused with these two.Concerning the fact that i add my URL at the finalURl definition. – Dimitris Koumouras Sep 28 '12 at 15:22
  • Ok i found out how to declare these two. I changed mimeType with @"mimeType" and i managed to establish connection. The bad news are that i get a 415 error which means that input is not recognized. Any idea?:) thanks in advance – Dimitris Koumouras Sep 28 '12 at 16:21