我正在尝试将字符串转换为url但未获得成功。任何更好的方法,或改进下面的代码。感谢
NSString *strURL = @"http://domain.com/This Is My, Code for This. app";
NSString *escapedUrlString = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(escapedUrlString);
显示此日志
http://domain.com/This2s2y,芙赋
0.000000or2his.20x1.55e800047f4p-1029pp
这是我的实际代码发送和保存数据到mysql
NSString *data = @"This Is My, Code for This. app";
NSString *strURL = [NSString stringWithFormat:@"http://domain.com/connection.php?data=%@", data];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSURL *urll = [NSURL URLWithString:strResult];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:urll];
[request setHTTPMethod:@"GET"];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData: %@", responseData);
没有成功,但是当我使用
时data = @"ThisIsMyCode";
然后一切正常...... 帮助
答案 0 :(得分:8)
NSString *data = @"This Is My, Code for This. app";
NSString *escapedDataString = [data stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *strURL = [NSString stringWithFormat:@"http://domain.com/connection.php?data=%@", escapedDataString];
NSLog(@"Encoded:%@",strURL);
这很好用,这是日志:
Encoded:http://domain.com/connection.php?data=This%20Is%20My,%20Code%20for%20This.%20app