NSData 到 NSString 使用 åöä

发布于 2024-12-20 20:03:02 字数 631 浏览 5 评论 0原文

我正在使用 NSMutableURLRequest 下载网页,但将该网页放入 NSString 时遇到问题。

NSString *username = @"my_username";

NSString *password = @"my_password";

NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.mypage.com/login.php?username=%@&password=%@", username, password]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSString *returnPage = [NSString stringWithFormat:@"%.*s", [returnData length], [returnData bytes]];

除了像 åäö 等特殊字符外,这工作得很好。有没有更好的方法?

I'm downloading a webpage using NSMutableURLRequest but having problem putting that very webpage into a NSString.

NSString *username = @"my_username";

NSString *password = @"my_password";

NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.mypage.com/login.php?username=%@&password=%@", username, password]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSString *returnPage = [NSString stringWithFormat:@"%.*s", [returnData length], [returnData bytes]];

This works fine except for special chars like åäö etc. Is there any better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的心爱 2024-12-27 20:03:02

是的,使用以下内容:

NSString *returnPage = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];

使用 UTF8 而不是 ASCII。

Yes, use the following:

NSString *returnPage = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];

That uses UTF8 instead of ASCII.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文