gpt4 book ai didi

objective-c - IOS 应用程序将数据发送到 REST API 服务内容类型定义错误

转载 作者:行者123 更新时间:2023-11-29 04:23:37 26 4
gpt4 key购买 nike

我成功地创建了一个应用程序来将数据发送到 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 有问题。有什么帮助吗??

提前致谢

最佳答案

好吧,我终于找到了。正是这个特定的行导致了问题,并且必须对内容类型执行某些操作:

contentType = @"application/x-www-form-urlencoded; charset=utf-8";

删除后,我能够将我的字符串正确发送到服务器。

关于objective-c - IOS 应用程序将数据发送到 REST API 服务内容类型定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12654299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com