带有包含特殊字符的 url 的 NSURLRequest

编程入门 行业动态 更新时间:2024-10-22 23:32:43
本文介绍了带有包含特殊字符的 url 的 NSURLRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到了同样的问题:带有特殊字符的 NSURL

但是尝试了他们的解决方案.无法让我的 NSURLRequest 使用 åöä 字符.如果变量string"包含 åöä,则请求返回 null.还尝试使用 NISOLatin1StringEncoding.

But tried their solution. Can't get my NSURLRequest to work with åöä characters. If the variable "string" contains åöä the request return null. Also tried with NSISOLatin1StringEncoding.

NSString *encodedString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *urlString = [NSString stringWithFormat: @"suggestqueries.google/complete/search?output=firefox&q=%@", encodedString]; NSURL *url = [NSURL URLWithString: urlString]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

这有效:suggestqueries.google/complete/search?output=firefox&q=%C3%A5%C3%B6%C3%A4 (åöä)

有什么想法吗?

使用调试器,NSURL 看起来是正确的:

Using the debugger the NSURL looks correct:

string __NSCFString * @"åäö" 0x0a77cd60 encodedString __NSCFString * @"%C3%A5%C3%A4%C3%B6" 0x0a77fc40 url NSURL * @"suggestqueries.google/complete/search?output=firefox&q=%C3%A5%C3%A4%C3%B6" 0x0a79d1f0

已解决:问题不是 NSURL,而是返回 NSDATA 的解释方式.

Solved: The problem was not the NSURL it was how the return NSDATA was interpreted.

推荐答案

代替使用

NSURL *url = [NSURL URLWithString: urlString]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

尝试使用

NSString *encoded = [urlString stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]]; NSURLRequest* request = [NSURLRequest requestWithURL: [NSURL URLWithString: encoded]];

更多推荐

带有包含特殊字符的 url 的 NSURLRequest

本文发布于:2023-10-29 10:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539401.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:特殊字符   url   NSURLRequest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!