gpt4 book ai didi

ios - Http Post参数到url不要在objectC Xcode中发送特殊字符 "+"

转载 作者:行者123 更新时间:2023-11-28 19:49:16 24 4
gpt4 key购买 nike

我创建了一个应用程序,它向 aspx 服务器发送两个 POST 参数,并将数据保存在数据库中。这是一个 Iphone 应用程序。

代码如下:

        NSString *post = [NSString stringWithFormat:@"name=%@&number=%@", 
name,number];
NSString *capturedpost = [post
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *postData = [capturedpost dataUsingEncoding:NSUTF8StringEncoding];
//NSString *postLength = [NSString stringWithFormat:@"%d", [postData
length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http:/www.myurl.aspx"]];
[request setHTTPMethod:@"POST"];
//[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
//[request setValue:@"application/x-www-form-urlencoded"
forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *requestResponse;
NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request
returningResponse:&requestResponse error:nil];
NSString *requestReply = [[NSString alloc] initWithBytes:[requestHandler
bytes] length:[requestHandler length] encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);
NSData *returnData = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];

但是在服务器上,包含电话号码的字符串“number”如“+393333...”在数据库中保存没有“+”的号码。我能怎么做?服务器端工作正常,因为在 Android 上执行相同请求的相同应用程序工作完美!

最佳答案

好的,多亏了“holex”,我以这种方式成功地对 URL 进行了编码:

 NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)post,
NULL,
(CFStringRef)@"+",
kCFStringEncodingUTF8 ));

并且“+”正确保存在数据库中!

关于ios - Http Post参数到url不要在objectC Xcode中发送特殊字符 "+",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30232578/

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