ios - 自动添加的 URL 末尾的 AFNetworking 斜杠
<p><p>AFNetworking v.1.x 的下一个问题可能与 2.x 相同</p>
<pre><code>#define LOGIN_URL @"http://myserverr.com/login"
NSURL *url = ];
AFHTTPClient *httpClient = [ initWithBaseURL:url ];
[httpClient postPath:nil
parameters:@{EMAIL_KEY : email,
PASSWORD_KEY : password}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
id json = ;
NSDictionary *result = (NSDictionary *)json;
;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@": %@", error.localizedDescription);
dispatch_async(dispatch_get_main_queue(), ^{
}];
</code></pre>
<p>但结果我的请求将发送到 URL <a href="http://myserverr.com/login/" rel="noreferrer noopener nofollow">http://myserverr.com/login/</a>而不是 <a href="http://myserverr.com/login" rel="noreferrer noopener nofollow">http://myserverr.com/login</a>最后一个斜线在我找到的文档中自动添加 </p>
<pre><code> // Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
if ([ length] > 0 && ![ hasSuffix:@"/"]) {
url = ;
}
</code></pre>
<p>但这对我没有帮助:)</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>鉴于这种行为,您可以将 URL 拆分为 <code>BASE_URL</code> 和 <code>LOGIN_PATH</code>:</p>
<pre><code>#define BASE_URL @"http://myserverr.com/"
#define LOGIN_PATH @"login"
NSURL *baseURL = ;
AFHTTPClient *httpClient = [ initWithBaseURL:baseURL];
[httpClient postPath:LOGIN_PATH
parameters:@{EMAIL_KEY : email,
PASSWORD_KEY : password}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// ...
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// ...
}];
</code></pre></p>
<p style="font-size: 20px;">关于ios - 自动添加的 URL 末尾的 AFNetworking 斜杠,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/20218859/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/20218859/
</a>
</p>
页:
[1]