用于希伯来字符的IOS HTTP Post编码

Edd*_*ddy 8 objective-c urlencode http-post

我需要从我的iPhone应用程序到谷歌文档进行HTTP发布.它适用于英语,但希伯来语出现了所有???????? 在谷歌文档中.

这就是我正在做的事情:

NSString *post = [Util append:@"&entry.xxxxx=", self.firstName.text, @"&entry.yyyyyyy=", self.phone.text, @"&entry.zzzzzzzz=", self.email.text, nil];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https://docs.google.com/forms/d/FORM_ID/formResponse"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

编辑:我试过这里提供的解决方案,看看ahmedalkaf的链接,但没有运气.

Tim*_*eit 3

选项1

您已将 Content-Type 设置为application/x-www-form-urlencoded,但尚未对内容进行 url 编码。

对您的帖子 NSString 进行 URL 编码并将编码更改为 UTF-8 (我不能告诉您原因,但需要它才能正常工作)应该可以完成这项工作。

NSString *post = [Util append:@"&entry.xxxxx=", self.firstName.text, @"&entry.yyyyyyy=", self.phone.text, @"&entry.zzzzzzzz=", self.email.text, nil];
post =[post stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
Run Code Online (Sandbox Code Playgroud)

其他一切都可以保持不变。但是您应该考虑使用异步请求。使用同步请求时,您的用户界面将变得无响应,直到请求完成。异步请求不会发生这种情况。

选项2

对于 HTTP 请求,我通常使用 ASIHTTPRequest 库:http://allseeing-i.com/ASIHTTPRequest/

集成到您的项目中需要几分钟,但它使一切变得更加简单。您不必搞乱编码等。

NSURL *url =[NSURL URLWithString:@"https://docs.google.com/forms/d/FORM_ID/formResponse"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

[request addPostValue:self.firstName.text forKey:@"entry.xxxx"];
[request addPostValue:self.phone.text forKey:@"entry.yyyy"];
[request addPostValue:self.email.text forKey:@"entry.zzzz"];

[request startAsynchronous];
Run Code Online (Sandbox Code Playgroud)

就是这样。

要设置 ASIHTTPRequest,请按照以下说明进行操作:http://allseeing-i.com/ASIHTTPRequest/Setup-instructions,如果您使用 ARC,请记住在“项目设置”->“构建阶段”->“编译源”-fno-objc-arc下的库文件中添加编译器标志。