iOS HTTP Post重定向处理程序不工作



我是iOS开发新手。我只是试图做一个post请求到服务器,但遇到了这里提到的问题与服务器重定向。我使用了答案中提到的事件处理程序,但事情仍然不能正常工作。

下面是我的。m代码:
@interface ViewController ()
@end
@implementation ViewController

#pragma mark NSURLConnection Delegate Methods

//CALL BACK METHODS
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"    didReceiveResponse");
    // A response has been received, this is where we initialize the instance var you created
    // so that we can append data to it in the didReceiveData method
    // Furthermore, this method is called each time there is a redirect so reinitializing it
    // also serves to clear it

    //initialize response
    _responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"   didReceiveData");
    // Append the new data to the instance variable you declared
    [_responseData appendData:data];

}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                  willCacheResponse:(NSCachedURLResponse*)cachedResponse {
    // Return nil to indicate not necessary to store a cached response for this connection
    return nil;
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"   connectionDidFinishLoading ");
    // The request is complete and data has been received
    // You can parse the stuff in your instance variable now
    NSString *dataReceived= [[NSString alloc] initWithData:_responseData encoding:NSUTF8StringEncoding];
    NSLog(@"    async response data: %@", dataReceived);

}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"   didFailWithError");
    // The request has failed for some reason!
    // Check the error var
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSString *post = [NSString stringWithFormat:@"&j_username=%@&j_password=%@",@"usrname",@"pw"];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    request = [[NSMutableURLRequest alloc] init];
    request.HTTPMethod= @"POST";
    //parameters
    [request setURL:[NSURL URLWithString:@"url"]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"XMLHttpRequest" forHTTPHeaderField:@"X-Requested-With"];
    [request setHTTPBody:postData];


    // Send a synchronous request
    if (0) {
        NSURLResponse * response = nil;
        NSError * error = nil;
        NSData * data = [NSURLConnection sendSynchronousRequest:request
                                              returningResponse:&response
                                                          error:&error];
        NSLog(@"  Synchronous request done");
        if (error == nil)
        {
            // Parse data here
            NSLog(@"     Synchronous response has no error");
            NSLog(@"    Synchronous Reply: %@", response);
        }
    }
    else {
        // Send Asynchronous request
        //NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [NSURLConnection connectionWithRequest:request delegate:self];
        NSLog(@"  Asynchronous request sent");
    }

}
- (NSURLRequest *)connection: (NSURLConnection *)connection
             willSendRequest: (NSURLRequest *)inRequest
            redirectResponse: (NSURLResponse *)redirectResponse;
{
    if (redirectResponse) {
        // we don't use the new request built for us, except for the URL
        NSURL *newURL = [request URL];
        NSString *redirectURL= [newURL absoluteString];
        NSLog(@"Redirect URL: ");
        NSLog(redirectURL);
        // Previously, store the original request in _originalRequest.
        // We rely on that here!
        NSMutableURLRequest *newRequest = [request mutableCopy];
        [newRequest setURL: newURL];
        NSLog(@"redirect occur");
        return newRequest;
    } else {
        NSLog(@"no redirect");
        return inRequest;
    }
}
@end

如果没有处理程序,请求将顺利通过(只是没有附加body);但是有了处理程序,重定向会被一次又一次地检测到,因为重定向的url与原始url相同。由于重定向过多,请求最终死亡。我认为这可能是一个服务器端问题,但我做了什么错误的编码,导致这一点?

基本上问题是redirectResponse的url不是你被重定向到的地方;它还是你在原始post方法中设置的那个。这就是为什么你会一次又一次地被重定向到相同的url。

你要做的是拦截你在响应头中被重定向到的url。在初始post请求被执行之后,您应该得到这样的响应头:

HTTP/1.1 302 Found
Location: http://www.iana.org/domains/example/

,其中"Location"表示您被重定向到的位置。所以像这样获取url:

NSDictionary* headers = [(NSHTTPURLResponse *)redirectResponse allHeaderFields];
NSString newUrl=headers[@"Location"];
在你的newRequest中使用newUrl,那么你应该很好。

相关内容

最新更新