0

我的 NSURLComponents 正在将 '/' 转换为 '%2F'。请查看 10.32.135.10 之后的日志。

NSURLComponents *urlComponent = [[NSURLComponents alloc] init];
[urlComponent setScheme:@"http"];
[urlComponent setHost:@"10.32.135.10/test.API"];
[urlComponent setPath:@"/api/path"];
urlComponent.queryItems = qItems;

在我执行时登录

po [urlComponent URL]
http://10.32.135.10%2Ftest.API/api/path?asetTypeId=1003

为什么反斜杠被翻译成 %2F。请指导。

4

1 回答 1

1

因为/test.API应该在path.

让我们换个角度想一想:

NSString *urlStr = @"http://10.32.135.10/test.API/api/path?asetTypeId=1003";
NSURLComponents *components = [[NSURLComponents alloc] initWithString:urlStr];
NSLog(@"Host: %@", [components host]);
NSLog(@"Path: %@", [components path]);
NSLog(@"QueryItems: %@", [components queryItems]);

输出:

$>Host: 10.32.135.10
$>Path: /test.API/api/path
$>QueryItems: (
    "<NSURLQueryItem 0x600000254760> {name = asetTypeId, value = 1003}"
)

使固定:

[urlComponent setHost:@"10.32.135.10"];
[urlComponent setPath:@"/test.API/api/path"];
于 2021-04-06T16:29:10.613 回答