获取iOS 5中的最后一个重定向网址? [英] Get the last redirected url in iOS 5?

查看:99
本文介绍了获取iOS 5中的最后一个重定向网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以发贴最简单的工作代码,当我获取请求网址时,该代码可以获取最后一个重定向的网址(第n个)吗?



我知道我需要使用异步请求,但我无法找到解决问题的完整工作代码。



我正在使用ios5,因此我可以在ios5中使用最新添加的异步内置功能。<​​/ p>

我试过很多事情,但我还没有成功。使用下面的代码我试图发送带参数的get请求但不知何故这些参数由于站点的重定向而丢失:(!

  NSURLRequest * request = [NSURLRequest requestWithURL:urlOriginal]; 

[NSURLConnection
sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *响应,NSData *数据,NSError *错误)
{
NSLog(返回的数据应该是带参数的重定向网址的最终数据,但不是);
}];

}

编辑



所以这就是我现在所做的:

  NSURLRequest * requestOriginal = [NSURLRequest requestWithURL:urlOriginal]; 

NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:requestOriginal delegate:self];

[connection start];

和委托:

   - (NSURLRequest *)连接:(NSURLConnection *)连接
willSendRequest:(NSURLRequest *)请求
redirectResponse:(NSURLResponse *)redirectResponse
{

if(redirectResponse){
NSMutableURLRequest * r = [request mutableCopy]; //原始请求
[r setURL:[request URL]];
返回r;
} else {
NSLog(@重定向到:%@,[请求URL]);
退货申请;
}
}

并且日志给了我正确的请求网址需要与最初传递的get参数一起使用。



现在如何从此请求中获取数据?另一个completionHandler委托方法?



edit2:



好的,所以我也实现了其他的delgates:




  • (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response


  • (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data


  • (void)connection:(NSURLConnection * )connection didFailWithError:(NSError *)error


  • (void)connectionDidFinishLoading:(NSURLConnection *)connection



解决方案

请阅读处理文档中的重定向和其他请求更改。简而言之,您需要a)为您的连接提供委托,并且b)在所述委托中实现方法 connection:willSendRequest:redirectResponse:。我不明白你在问题中想要做什么 - 你只是想要重定向连接的URL中的数据,还是想看到重定向的请求?无论哪种方式,实施上述方法都应该有所帮助。您的代理人将能够看到重定向的请求,并且不更改它将导致连接加载新URL。


Can someone please post the simplest of working code that can get the last redirected url(nth) when I GET request a url?

I know I need to use asynchronous requests but I am not able to work out a complete working code that solves the problem.

I am using ios5 so I can use the latest added asynchronous inbuilt functionalities in ios5.

I have tried a lot of things but I am not successful still. Using the below code I am trying to send a get request with parameters but somehow those parameters are getting lost due to site's redirection :(!

NSURLRequest *request = [NSURLRequest requestWithURL:urlOriginal];

[NSURLConnection 
sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
{
    NSLog("the returned data should be the final data of the redirected url with parameters but it is not");
}];

}

EDIT

So this is what I have done now :

NSURLRequest *requestOriginal = [NSURLRequest requestWithURL:urlOriginal];

 NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:requestOriginal delegate:self];

[connection start];

and delegate :

-(NSURLRequest *)connection:(NSURLConnection *)connection
        willSendRequest:(NSURLRequest *)request
       redirectResponse:(NSURLResponse *)redirectResponse
{

    if (redirectResponse) {
    NSMutableURLRequest *r = [request mutableCopy]; // original request
    [r setURL: [request URL]];
    return r;
} else {
     NSLog(@"redirecting to : %@", [request URL]);
    return request;
}
}

and the log gives me the correct request url that i need along with the originally passed get parameters.

Now how to I get the data from this request? Another completionHandler delegate method?

edit2 :

Ok so I implemented the other delgates as well :

  • (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

  • (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

  • (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

  • (void)connectionDidFinishLoading:(NSURLConnection *)connection

解决方案

Please read Handling Redirects and Other Request Changes in the documentation. Briefly, you'll want to a) provide a delegate for your connection, and b) implement the method connection:willSendRequest:redirectResponse: in said delegate. I don't understand exactly what you're trying to do in your question -- do you just want the data from URL to which your connection is redirected, or do you want to see the redirected request? Either way, implementing the method above should help. Your delegate will be able to see the redirected request, and returning it unchanged should cause the connection to load the new URL.

这篇关于获取iOS 5中的最后一个重定向网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆