如何在ios中请求包含空格和圆点的url?

如何在ios中请求包含空格和圆点的url?,ios,json,namespaces,nsdata,nsurl,Ios,Json,Namespaces,Nsdata,Nsurl,我试图请求下面的URL,但我收到一个错误,说“数据参数为零”。我发现“卖方名称”有空格,“金额”有点(.)。我认为这就是URL导致的问题。那么,有没有什么方法可以在不丢失信息的情况下发送有空格和圆点的URL呢 NSURL *url1 = [[NSURL alloc]initWithString:[NSString stringWithFormat:@"192.168.1.85/localex_postsale.html?contactid=%@&exchangeid=%@&tok

我试图请求下面的URL,但我收到一个错误,说“数据参数为零”。我发现“卖方名称”有空格,“金额”有点(.)。我认为这就是URL导致的问题。那么,有没有什么方法可以在不丢失信息的情况下发送有空格和圆点的URL呢

NSURL *url1 = [[NSURL alloc]initWithString:[NSString stringWithFormat:@"192.168.1.85/localex_postsale.html?contactid=%@&exchangeid=%@&token=%@&buyerid=%@&seller_name=%@&desc=%@&amount=%@&sellerid=%@",contactid,exchangeid,token,buyervalue,sellers,_Description,_Amount,contactid]];
NSError *errors1;
NSData *data1 = [NSData dataWithContentsOfURL:url1];
NSDictionary *json1 = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:&errors1];

查看
NSString
中的
-stringbyaddingpercentescapesusingencode:
方法,例如:


参考:Apple。

之所以发生这种情况,是因为URL不应包含空格。如果有空格,则应将其编码为
%20
。我们可以使用
NSUTF8StringEncoding
NSString
中的空格和特殊字符进行编码,如下所示

NSString *string = [[NSString stringWithFormat:@"192.168.1.85/localex_postsale.html?contactid=%@&exchangeid=%@&token=%@&buyerid=%@&seller_name=%@&desc=%@&amount=%@&sellerid=%@",contactid,exchangeid,token,buyervalue,sellers,_Description,_Amount,contactid]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url1 = [[NSURL alloc]initWithString:string];
NSError *errors1;
NSData *data1 = [NSData dataWithContentsOfURL:url1];
NSDictionary *json1 = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:&errors1]; 
NSString *string = [[NSString stringWithFormat:@"192.168.1.85/localex_postsale.html?contactid=%@&exchangeid=%@&token=%@&buyerid=%@&seller_name=%@&desc=%@&amount=%@&sellerid=%@",contactid,exchangeid,token,buyervalue,sellers,_Description,_Amount,contactid]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url1 = [[NSURL alloc]initWithString:string];
NSError *errors1;
NSData *data1 = [NSData dataWithContentsOfURL:url1];
NSDictionary *json1 = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:&errors1];