自动添加的 URL 末尾的 AFNetworking 斜杠
Posted
技术标签:
【中文标题】自动添加的 URL 末尾的 AFNetworking 斜杠【英文标题】:AFNetworking slash at the end of URL that added automatically 【发布时间】:2013-11-26 13:49:27 【问题描述】: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:@""];
但这对我没有帮助:)
【问题讨论】:
你不能用NSURLConnection
代替吗?
还有..也许这可以帮助你:github.com/AFNetworking/AFNetworking/issues/994
【参考方案1】:
鉴于这种行为,您可以将 URL 拆分为 BASE_URL
和 LOGIN_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)
// ...
];
【讨论】:
对我很有帮助)谢谢以上是关于自动添加的 URL 末尾的 AFNetworking 斜杠的主要内容,如果未能解决你的问题,请参考以下文章