Question

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.

Was it helpful?

Solution

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top