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

标签 ios post nsurlconnection afnetworking

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/

相关文章:

iphone - NSURLConnection 的内存管理

swift - 无法将 URL.Type 类型的值转换为预期的参数类型 'URL'

iphone - UISearchBar 中的清除按钮

javascript - 简单的 javascript AJAX 发布功能不起作用

ios - 从 Git 检索核心数据托管对象模型

Perl 使用 HTTP::Request 上传文件

使用请求的python网络抓取帖子表单数据不起作用

objective-c - iOS,Objective C - NSURLConnection 和异步示例、默认行为和最佳实践?

ios - 单击 UICollectionViewCell 时创建新的 View Controller

ios - 在iTunes Connect上提交隐私政策