从xcode发出帖子请求iPhone->服务器

时间:2012-10-30 13:59:25

标签: iphone post

我做了很多搜索,但我发现的代码片段似乎都不适用于我。我在xcode中有以下片段可以在我的iPhone应用程序中运行:

NSURL *url = [NSURL URLWithString:@"http://blah.blah.com/rec.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSData *body = [@"data=crap" dataUsingEncoding:NSUTF8StringEncoding];
NSString *bodyLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:bodyLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];    
[request setHTTPBody:body];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];

rec.php被称为罚款,但是帖子数据没有通过。条目$ _POST ['data']为空。谁能让我知道我做错了什么?

0 个答案:

没有答案