简体   繁体   English

IOS Application将数据发送到Rest API服务

[英]IOS Application to send data to a Rest API service

I want to create an iPhone application to send data to a Rest API service. 我想创建一个iPhone应用程序来将数据发送到Rest API服务。 If the data is a string defined as geoX#35#geoY#65 which NSS method shall i use. 如果数据是定义为geoX#35#geoY#65的字符串,我将使用NSS方法。 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). 考虑NSString和NSMutablerequest来发出我的请求并定义我的字符串,但这不起作用。我也使用NSURLconnection建立与服务器的连接(也许这也是错误的)。 Anyone that can help ? 有人可以帮忙吗?

Thanks in advance. 提前致谢。

Your connection data is using special characters and if you try to do this with GET method of NSURLConnection. 您的连接数据使用特殊字符,如果您尝试使用NSURLConnection的GET方法执行此操作。 It will Shows connection error. 它将显示连接错误。 For this You have to use POST method like : 为此你必须使用POST方法,如:

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM