将西里尔字母作为 URL 参数传递

发布于 2024-09-27 01:12:47 字数 1021 浏览 1 评论 0原文

我需要在 iPhone 应用程序的 URL 中传递西里尔字符作为参数。示例 URL 如下所示:

http://www.mysite.com/script.php?message=страшная

当我在浏览器中使用此 URL 时,它会返回正确的结果。但是,在我的应用程序中,不喜欢西里尔字母,最终我在 didFailWithError 代码中得到了一个“错误的网址”。

我在浏览器中尝试了几种编码来解决这个问题,但没有成功。有没有办法通过 iPhone 应用程序实际发送上面的确切 URL 示例?我正在使用的代码片段是:

selectedWord = @"страшная";
NSString *requestURL = [NSString stringWithFormat:@"http://www.mysite.com/script.php?message=%@", selectedWord];
NSLog (requestURL);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:requestURL]];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

在上面的代码中,为了调试目的,我在 NSString *selectedWord 中对西里尔字母进行了硬编码。在实际应用中,它将根据用户输入动态变化。

我尝试了更复杂的POST,认为我可以将西里尔字母作为数据而不是文本传递,但要么我做错了,要么它不起作用。

我可以在浏览器中尝试任何有关编码的示例,然后在应用程序中实现都会有所帮助。我意识到西里尔字母在 NSURL 中被拒绝,但我希望有一种方法可以使用不同的代码或编码来解决它。

I need to pass Cyrillic characters as a parameter in a URL in my iPhone app. A sample URL looks like:

http://www.mysite.com/script.php?message=страшная

When I use this URL in my browser, it returns the correct result. However, in my app, the cyrillic is not liked, and I end up getting a "bad url" in the didFailWithError code.

I have tried several encodings in my browser to get around this, but with no luck. Is there a way to actually send the exact URL example above via an iPhone app? The snippet of code I am using is:

selectedWord = @"страшная";
NSString *requestURL = [NSString stringWithFormat:@"http://www.mysite.com/script.php?message=%@", selectedWord];
NSLog (requestURL);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:requestURL]];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

In the above code, I have hardcoded the cyrillic word in the NSString *selectedWord for debug purposes. In the real application it will be dynamic based on user input.

I have tried a more complex POST, thinking that I could pass the cyrillic as data rather than text, but either I am doing it wrong, or it doesn't work either.

Any example regarding encoding I can try in my browser, then implement in the app would be helpful. I realize that the cyrillic is being rejected in the NSURL, but I was hoping there was a way around it with different code or encoding.

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

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

发布评论

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

评论(1

燕归巢 2024-10-04 01:12:47

如果将其更改为:

encodedSelectedWord = [selectedWord stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *requestURL = [NSString stringWithFormat:@"http://www.mysite.com/script.php?message=%@", encodedSelectedWord];

文档链接

What if you change it to:

encodedSelectedWord = [selectedWord stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *requestURL = [NSString stringWithFormat:@"http://www.mysite.com/script.php?message=%@", encodedSelectedWord];

documentation link

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