0

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

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

到一个较短的 URL,该 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

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

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

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

4

1 回答 1

0

那么实现这一目标的一种方法是:

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];
于 2013-07-16T08:06:11.127 回答