Php iOS-目标C-POST字符串转义/编码问题
经过大约一周的研究,没有解决方案,我放弃了,我向你们寻求建议 我需要相同的php脚本以相同的方式处理POST请求,而不管它们的来源是简单的HTML表单还是iOS应用程序。不涉及JSON 问题:当POST发件人是iOS应用程序时,某些字符丢失。在这一点上,我想说,我不知道为什么它会这样。显然,web浏览器在POST请求的构建过程中正确地逃逸了所有字符,而我的obj-c代码却没有 在objective-c代码中,您可以看到我所做的5个Tentive,并注意到出错的地方 我真的希望你能帮忙 tester.php文件:Php iOS-目标C-POST字符串转义/编码问题,php,ios,objective-c,nsurlconnection,Php,Ios,Objective C,Nsurlconnection,经过大约一周的研究,没有解决方案,我放弃了,我向你们寻求建议 我需要相同的php脚本以相同的方式处理POST请求,而不管它们的来源是简单的HTML表单还是iOS应用程序。不涉及JSON 问题:当POST发件人是iOS应用程序时,某些字符丢失。在这一点上,我想说,我不知道为什么它会这样。显然,web浏览器在POST请求的构建过程中正确地逃逸了所有字符,而我的obj-c代码却没有 在objective-c代码中,您可以看到我所做的5个Tentive,并注意到出错的地方 我真的希望你能帮忙 teste
<?php
header('Content-Type: text/html; charset=utf-8');
if (isset($_POST['email']))
{
echo "email: " . $_POST['email'];
echo "\n";
echo "password: " . $_POST['password'];
}
else
{
?>
<form method="post">
<input type="text" id="email" name="email" value="切换到中文@gmail.com" />
<br>
<input type="text" id="password" name="password" value="&+=/切/().%&" />
<br>
<input type="submit" />
</form>
<?php
}
如果我使用HTML表单(与我在表单中输入的内容完全相同),则返回结果:
我从objective c代码(一些字符消失)中得到的,这是最后的测试之一:
2016-09-29 21:28:59.519 test[44752:2438179] Before: 切换到中文@gmail.com (15) and &+=/切/().%& (11)
2016-09-29 21:28:59.705 test[44752:2438721] email: 切换到中文@gmail.com password: &+=/切/().&
我就是这样做的。我试过的每件事都很管用。首先,您需要将该函数添加到NSString:
@implementation NSString (NSStringWebStructure)
-(NSString*)stringToWebStructure
{
NSString* webString = [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
webString = [webString stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
webString = [webString stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
return webString;
}
@end
现在您需要请求函数:
-(NSString*)launchURL:(NSURL*)url withPostString:(NSString*)post
{
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (response.statusCode >= 200 && response.statusCode < 300)
{
NSString *responseData = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
if (responseData.length > 0) return responseData;
}
else if (error) return [error description];
return nil;
}
-(NSString*)launchURL:(NSURL*)url withPostValues:(NSDictionary*)postDict
{
NSArray* postKeys = postDict.allKeys;
NSMutableArray* postLines = [[NSMutableArray alloc] init];
for (NSString* key in postKeys)
{
[postLines addObject:[NSString stringWithFormat:@"%@=%@",key,[postDict[key] stringToWebStructure]]];
}
return [self launchURL:url withPostString:[postLines componentsJoinedByString:@"&"]];
}
stringByAddingPercentEscapesUsingEncoding:
在macOS 10.11中被弃用,但如果您打算像我一样支持旧的macOS版本,这是一种功能性方法(从macOS 10.6到macOS 10.12进行测试)。这是我的做法。我试过的每件事都很管用。首先,您需要将该函数添加到NSString:
@implementation NSString (NSStringWebStructure)
-(NSString*)stringToWebStructure
{
NSString* webString = [self stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
webString = [webString stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
webString = [webString stringByReplacingOccurrencesOfString:@"?" withString:@"%3F"];
return webString;
}
@end
现在您需要请求函数:
-(NSString*)launchURL:(NSURL*)url withPostString:(NSString*)post
{
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (response.statusCode >= 200 && response.statusCode < 300)
{
NSString *responseData = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
if (responseData.length > 0) return responseData;
}
else if (error) return [error description];
return nil;
}
-(NSString*)launchURL:(NSURL*)url withPostValues:(NSDictionary*)postDict
{
NSArray* postKeys = postDict.allKeys;
NSMutableArray* postLines = [[NSMutableArray alloc] init];
for (NSString* key in postKeys)
{
[postLines addObject:[NSString stringWithFormat:@"%@=%@",key,[postDict[key] stringToWebStructure]]];
}
return [self launchURL:url withPostString:[postLines componentsJoinedByString:@"&"]];
}
stringbyaddingpercentescapesusingencode:
在macOS 10.11中被弃用,但如果您打算像我一样支持旧的macOS版本,这是一种功能性方法(从macOS 10.6到macOS 10.12进行测试)
NSString *email = @"切换到中文@gmail.com";
NSString *password = @"&+=/切/().%&";
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/tester.php"];
NSDictionary* postValues = @{@"email":email, @"password": password};
NSString* response = [self launchURL:url withPostValues: postValues];
NSLog(@"%@",response);