【问题标题】:NSURL Bad URL error when using stringByAddingPercentEscapesUsingEncoding使用 stringByAddingPercentEscapesUsingEncoding 时出现 NSURL Bad URL 错误
【发布时间】:2013-07-11 08:14:30
【问题描述】:

我将两个字符串连接起来得到一个URL,下面是连接字符串的方法: 这样做的目的是从一个 URL 即:

  • https://vula.uct.ac.za/portal/pda/9f563cb2-24f9-481f-ab2e-631e85c9f3aa/tool-reset/10f71bdb-24b9-4060-bf6d-2c9654253aa3

转至仅显示部分网页的较短 URL,并且是:

  • https://vula.uct.ac.za/portal/tool-reset/10f71bdb-24b9-4060-bf6d-2c9654253aa3

    -(NSString *)cropURL:(NSString *)inputURL{
    NSString *outputURL ;
    NSRange match;
    match = [inputURL rangeOfString: @"tool-reset"]; //Gives the index and range of the string "tool-reset"
    int toolLen = match.location+match.length ; //Gives the index after "tool-reset"
    NSString*tempIdentifier = [inputURL substringFromIndex:toolLen] ; //Gets the characters after "tool-reset"
    NSString *firstURL = @"https://vula.uct.ac.za/portal/tool-reset" ; //First part of URL
    NSMutableArray *concatURL = [[NSMutableArray alloc] initWithCapacity:2] ; // Array to concat firstURL and tempIdentifier
    [concatURL addObject:(NSString *)firstURL] ;
    [concatURL addObject:(NSString *) tempIdentifier] ;
    outputURL = [concatURL componentsJoinedByString:@""] ; //Concatenatd to outputURL
    outputURL = [outputURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ; //Encoding
    return outputURL ;
    

    }

*请注意,网站需要用户登录,这是较早完成的,只有登录成功后才会调用这些方法。

我使用 GET 将此连接的 URL 发送到连接方法,但出现以下错误:

Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x1d51f7d0 {NSUnderlyingError=0x1e547980 "bad URL", NSLocalizedDescription=bad URL}

但未连接的 URL 没有这个问题。

有问题的网址是:

https://vula.uct.ac.za/portal/tool-reset/10f71bdb-24b9-4060-bf6d-2c9654253aa3

该 URL 正确且已经过测试。 (再次注意,该网站要求您登录)

我确实使用过:

stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding

在将其发送到连接之前,但我仍然遇到同样的错误。

我的所有其他链接都有效,唯一无效的链接是连接的链接。这些连接起来的字符串确实可以在普通的网络浏览器中使用。

有什么想法可能是错的吗?

【问题讨论】:

  • @ali-hen 你能粘贴你的连接代码吗?
  • @ali-hen 你把字符串转换成NSURL格式了吗?
  • @icodebuster 是的,我在请求的连接方法中使用了... initWithURL: [NSURL URLWithString:currentURL] ... currentURL 是有问题的 URL。
  • @ali-hen 你能发布一个你传递给cropUrl函数的示例字符串
  • @icodebuster 示例字符串是上述问题中的第一个 URL:vula.uct.ac.za/portal/pda/9f563cb2-24f9-481f-ab2e-631e85c9f3aa/…

标签: nsurl nsurlerrordomain


【解决方案1】:

实现这一目标的一种方法:

NSURL *input = [NSURL URLWithString:@"https://vula.uct.ac.za/portal/pda/9f563cb2-24f9-481f-ab2e-631e85c9f3aa/tool-reset/10f71bdb-24b9-4060-bf6d-2c9654253aa3"];

NSString *identifier = input.lastPathComponent;

NSURL *base = [NSURL URLWithString:@"https://vula.uct.ac.za/portal/tool-reset/"];

NSURL *output = [base URLByAppendingPathComponent:identifier];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 2015-11-16
    • 2016-10-23
    相关资源
    最近更新 更多