![](/img/trans.png)
[英]IOS Application to send data to a REST API service content-type definition error
[英]IOS Application to send data to a Rest API service
我想創建一個iPhone應用程序來將數據發送到Rest API服務。 如果數據是定義為geoX#35#geoY#65的字符串,我將使用NSS方法。 考慮NSString和NSMutablerequest來發出我的請求並定義我的字符串,但這不起作用。我也使用NSURLconnection建立與服務器的連接(也許這也是錯誤的)。 有人可以幫忙嗎?
提前致謝。
您的連接數據使用特殊字符,如果您嘗試使用NSURLConnection的GET方法執行此操作。 它將顯示連接錯誤。 為此你必須使用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.