drrw8672 2013-03-01 15:29
浏览 59
已采纳

Objective-C发送特殊字符POST

I have create a application that send two POST parameter to my file PHP and it's save data in my database.

The code into xcode project that send parameter is:

NSString *post = [[NSString alloc] initWithFormat:@"&key1=%@&key2=%@", myString1, myString2];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.mysite.com/test.php"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

The code into my file PHP that save data into my database is:

$val1 = $_POST['key1'];
$val2 = $_POST['key2'];
$val1 = urldecode($val1);
$val2 = urldecode($val2);

$sql = "INSERT INTO test(nome, cognome) VALUES ('$val1', '$val2')";
mysql_query($sql);

The problem is that if I insert a parameter that contains special character (for example é or ä), my program save into database normal character (e and a).

Can someone help me with this?

  • 写回答

1条回答 默认 最新

  • doujie7886 2013-03-01 15:32
    关注
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    

    The problem here is that you're using ASCII, but ASCII doesn't contain é, ä, etc. You should be using Unicode instead. Try switching NSASCIIStringEncoding for NSUTF8StringEncoding in the code above.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题