如何获取域名?

时间:2011-05-11 09:09:15

标签: iphone

大家好我有一个阵列,我正在获取

等网站的链接
http://clickfrom.buy.com/default.asp?adid\u003d17379&sURL\u003dhttp%3A%2F%2Fwww.buy.com%2Fprod%2Fkimberly-clark-professional-kleenex-boutique-white-facial-tissue-2-ply%2Fq%2Floc%2F66357%2F211347223.html

我想要的只是获得主域名。 clickfrom.buy.com

主要网站地址。 如何实现......

3 个答案:

答案 0 :(得分:11)

如果你有NSURL,你可以要求

[url host]

这应该与上面的“clickfrom.buy.com”相对应。

答案 1 :(得分:3)

你可以用:

NSString *myUrl = @"http://clickfrom.buy.com/default.asp?adid\\u003d17379&sURL\\u003dhttp%3A%2F%2Fwww.buy.com%2Fprod%2Fkimberly-clark-professional-kleenex-boutique-white-facial-tissue-2-ply%2Fq%2Floc%2F66357%2F211347223.html";

NSURL *url = [NSURL URLWithString:myUrl];

NSLog(@"%@", [url host]);

修改

序列

\u003d
NSString中的

被解释为通用字符,因此必须引用我们的示例:

\\u003d

并且会导致错误: \ u003d不是有效的通用字符。仍然存在解析问题,因为反斜杠似乎是URI语法中的无效字符。它必须被百分比逃脱。

查看RFC 2396 / 2.4.3。排除的US-ASCII字符

...
Other characters are excluded because gateways and other transport
agents are known to sometimes modify such characters, or they are
used as delimiters.

unwise      = "{" | "}" | "|" | "\" | "^" | "[" | "]" | "`"

Data corresponding to excluded characters must be escaped in order to
be properly represented within a URI.

这样做

NSString *myUrl = [@"http://clickfrom.buy.com/default.asp?adid\\u003d17379&sURL\\u003dhttp%3A%2F%2Fwww.buy.com%2Fprod%2Fkimberly-clark-professional-kleenex-boutique-white-facial-tissue-2-ply%2Fq%2Floc%2F66357%2F211347223.html" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

应该让解析工作。

答案 2 :(得分:0)

使用rangeOfString的{​​{1}}方法获取您的域的边界,然后使用NSString的{​​{1}}方法提取域文本。

substringWithRange