iphone:NSMutableURLRequest 返回 MS Word 样式撇号的奇怪字符
我们使用 XML/NSMutableURLRequest 从我们的网站中提取内容,有时它会通过“卷曲”样式的撇号和引号(“而不是”)提取内容。 NSMutableURLRequest 似乎讨厌这些并将它们变成奇怪的 \U00e2\U0080\U0099 字符串。
我可以采取什么措施来防止这种情况发生吗?我正在使用 GET 方法,那么我应该以某种方式告诉它使用 UTF-8 吗?或者,我错过了什么吗?
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
NSString *urlStr = [NSString stringWithFormat:@"%@",url];
NSURL *serviceUrl = [NSURL URLWithString:urlStr];
NSMutableURLRequest *serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl];
[serviceRequest setHTTPMethod:@"GET"];
NSURLResponse *serviceResponse;
NSError *serviceError;
app.networkActivityIndicatorVisible = NO;
return [NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&serviceResponse error:&serviceError];
We are pulling content off our website using XML/NSMutableURLRequest and sometimes it pulls through the "curly" style apostrophe and quotes, ’ rather than '. NSMutableURLRequest seems to hate these and turns them into the strange \U00e2\U0080\U0099 string.
Is there something that I can to do prevent this? I am using the GET method, so should I be somehow telling it to use UTF-8? Or, am I missing something?
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
NSString *urlStr = [NSString stringWithFormat:@"%@",url];
NSURL *serviceUrl = [NSURL URLWithString:urlStr];
NSMutableURLRequest *serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl];
[serviceRequest setHTTPMethod:@"GET"];
NSURLResponse *serviceResponse;
NSError *serviceError;
app.networkActivityIndicatorVisible = NO;
return [NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&serviceResponse error:&serviceError];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSURLConnection
返回一个NSData
响应。您可以获取 NSData 响应并将其转换为字符串。然后获取该字符串,将其转回 NSData 对象,一路上对其进行正确的 UTF-8 编码,并将其提供给 NSXMLParser。示例:(假设
response
是您请求的NSData
响应)NSURLConnection
returns anNSData
response. You can take thatNSData
response and turn it into a string. Then take this string, turn it back into aNSData
object, properly UTF-8 encoding it along the way, and feed it toNSXMLParser
.Example: (Assuming
response
is theNSData
response from your request)我建议使用 stringByReplacingCharactersInRange:withString: 替换这些字符来替换不需要的字符串。
我过去曾这样做过,以解决您所描述的相同问题。
I would suggest replacing those characters using
stringByReplacingCharactersInRange:withString:
to replace the unwanted strings.I've done this in the past to handle the same problem you describe.
尝试使用 stringByReplacingPercentEscapesUsingEncoding:
Try using the stringByReplacingPercentEscapesUsingEncoding: