将参数发送到webservice

时间:2017-08-22 08:23:50

标签: ios objective-c web-services

我需要在webservice中向objective c发送法语字符串“Commentaire d'arret”。 但是当我将此字符串作为参数发送给服务时,我的应用程序崩溃了。像“india”这样的普通字符串工作正常。

任何人都可以帮助我。

3 个答案:

答案 0 :(得分:2)

为GET请求编码参数:

let param = "Commentaire d’arret"
let encodedParam = (param as NSString).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

结果评论%20d%E2%80%99arret

如果您想发送POST请求,请按照以下方式准备您的帖子:

let postString = "param=consultés"
let postData = postString.data(using: .utf8)

<强>目标C

NSString *param = @"Commentaire d’arret";
NSString *encodedParam = [param stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

NSString *postString = @"param=consultés";
NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding];

答案 1 :(得分:-1)

正如您所说,我已检查过您的代码,您需要在转换为您的网址之前进行编码。试试这个。

NSString *searchApi = [NSString stringWithFormat:@"xxx/xxx/index?
q=@consultés"];
searchApi = [searchApi stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *tempURL = [NSURL URLWithString:searchApi];

答案 2 :(得分:-1)

试试这个:

NSString *strParam = @"Commentaire d’arret";
NSString *encodedParam = [strParam stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];