Objective c iOS POST有时只工作

Objective c iOS POST有时只工作,objective-c,Objective C,我有以下代码: NSString *post = @"i hope this gets there"; NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]; NSString *postLength = [NSString stringWithFormat:@"%d", [post length]]; NSURL *url = [NSURL URLWithString

我有以下代码:

NSString *post = @"i hope this gets there";

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d", [post length]];

NSURL *url = [NSURL URLWithString:@"http://www.oneoftwosites.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];

[NSURLConnection connectionWithRequest:request delegate:nil];

//potential response
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

//convert response so that it can be LOG'd
NSString *returnString = [[NSString alloc] initWithData:response encoding:NSASCIIStringEncoding];

NSLog(@"RETURN: %@", returnString);
我已经在两个不同的网站上进行了测试:并且,用
http://www.oneoftwosites.com
。出于某种原因,它只适用于第一个网站(用perl编写),而不适用于第二个网站(用.php编写)

有人知道为什么会这样吗?这似乎很愚蠢,因为它有时只能工作,但我真的不知道为什么它不能为第二个网站工作(我没有看到代码,但我确实看到了一些示例输出)


编辑:我补充道


[请求设置值:@“应用程序/x-www-form-urlencoded”用于HttpHeaderField:@“内容类型”]
并尝试了几种不同的类型,但仍然没有成功

Instagraph需要什么样的POST数据?很可能是某种结构化数据,如URL编码的表单、XML或JSON。你的绳子不是这两条。因此,请提供
内容类型
,并相应地格式化您的帖子数据。也许你会有更好的运气

编辑:您的第二个站点是Instagraph。这是一个公共网站。它可能有一个文档化的API——在一篇文章中应该有哪些字段和值。您发送的POST数据是否符合该规范?到目前为止,我不这么认为。如果您发送虚拟的测试数据,为什么您希望它工作?你期望什么样的合理效果


顺便说一句,“我希望这一行到达目的地”并不构成类型为
application/x-www-form-urlencoded
的有效数据块。帖子长度应该来自postData

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

您可以尝试设置HTTP主机头吗?这是我过去遇到的一个问题。