0
我正面临一个大问题,HTTP Post方法php和ios
我想发送数据到我的服务器,我的数据是一个字符串。
我不希望使用GET方法,因为该字符串可能会很长,所以我想使用POST方法,但一切顺利,错了,如果有人能帮助我,他会是我心目中的英雄:)
这是我的PHP代码:
<?php include("config.inc.php");
if (isset($_POST['contentInterro']) && $_POST['contentInterro'] !="") {
//$id_user = $_POST['contentInterro'];
//$db->sql_query("INSERT INTO interrogations VALUES(DEFAULT, '$id_user')");
echo "succes";
}else{
echo "This is an error";
}
?>
这里是我的应用程序代码:
NSData *postData = [stringToPost2 dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *requestPost = [[NSMutableURLRequest alloc] init];
NSURL *urlPost = [NSURL URLWithString:@"http://buzznapps.fr/FDF/postInterrogation.php"];
[requestPost setURL:urlPost];
[requestPost setHTTPMethod:@"POST"];
[requestPost setValue:@"lol" forHTTPHeaderField:@"contentInterro"];
NSError *errorURL;
NSURLResponse *response;
NSData *urlData = [NSURLConnection sendSynchronousRequest:requestPost returningResponse:&response error:&errorURL];
NSString *str = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Str = %@",str);
我总是得到一个错误,我已经在网络上搜索,我不知道如何得到这个数据! 感谢您的帮助。
太谢谢你了! – user2252092
你如何使用@try?我已经将这段代码粘贴到一个NSObject类中,除了在程序中给出一个意外错误的@try行之外,一切都很好。 @ try @ catch必须进入方法吗? – marciokoko
@marciokoko:是的 – Pradeep