OStack程序员社区-中国程序员成长平台

标题: ios - 自动添加的 URL 末尾的 AFNetworking 斜杠 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:45
标题: ios - 自动添加的 URL 末尾的 AFNetworking 斜杠

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

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

NSURL *url = [NSURL URLWithStringOGIN_URL relativeToURL:[NSURL URLWithStringOGIN_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""];
    }

但这对我没有帮助



Best Answer-推荐答案


鉴于这种行为,您可以将 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 postPathOGIN_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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4