我设法制作了一个将数据发送到rest api服务的应用程序。内容类型必须是 text/html。但是每当我运行我的应用程序时,我都会收到 415 http 代码响应,这意味着不支持的内容类型。这是我的代码:
NSURL *url = [NSURL new];
NSData *body = nil;
NSString *contentType = @"text/html";
NSURL *finalURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://telesto.zapto.org:81/SMART_EdgeNode/EdgeNode/DataFeeds/3/addMeasurement"]];
NSString *yourString = @"geoX#35#geoY#65";
contentType = @"application/x-www-form-urlencoded; charset=utf-8";
body = [[NSString stringWithFormat:@"%@", yourString] dataUsingEncoding:NSUTF8StringEncoding];
NSString *putLength = [NSString stringWithFormat:@"%d",[body length]];
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:@"PUT"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:body];
[request setValue:putLength forHTTPHeaderField:@"Content-Length"];
self.conn = [NSURLConnection connectionWithRequest:request delegate:self];
我猜 atm 定义 content-type 有问题。有什么帮助吗??
提前致谢