gpt4 book ai didi

ios - 自动添加的 URL 末尾的 AFNetworking 斜杠

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:23 27 4
gpt4 key购买 nike

AFNetworking v.1.x 的下一个问题可能与 2.x 的问题相同

#define LOGIN_URL @"http://myserverr.com/login"

NSURL *url = [NSURL URLWithString:LOGIN_URL relativeToURL:[NSURL URLWithString:LOGIN_URL]];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url ];
[httpClient postPath:nil
parameters:@{EMAIL_KEY : email,
PASSWORD_KEY : password}
success:^(AFHTTPRequestOperation *operation, id responseObject) {

id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
NSDictionary *result = (NSDictionary *)json;
[DCDDownloadHelper loginResult:result];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
dispatch_async(dispatch_get_main_queue(), ^{

}];

但结果我的请求将转到 url http://myserverr.com/login/而不是 http://myserverr.com/login我在接下来找到的文档中自动添加了最后一个斜杠

 // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {
url = [url URLByAppendingPathComponent:@""];
}

但这对我没有帮助:)

最佳答案

鉴于该行为,您可以将您的 URL 拆分为 BASE_URLLOGIN_PATH:

#define BASE_URL   @"http://myserverr.com/"
#define LOGIN_PATH @"login"

NSURL *baseURL = [NSURL URLWithString:BASE_URL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[httpClient postPath:LOGIN_PATH
parameters:@{EMAIL_KEY : email,
PASSWORD_KEY : password}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// ...
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// ...
}];

关于ios - 自动添加的 URL 末尾的 AFNetworking 斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20218859/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com