user1673099
user1673099

Reputation: 3299

How to get Redirected URL

I have a URL.when i try to open it in browser, it will redirect to another URL & display the Content. I want that content But i don't get that redirected URL. So, I can't able to display data.

How can i do that programmatically??

e.g. URL which i have : http://www.windpowerengineering.com/?p=11020

& the redirected URL is: http://www.windpowerengineering.com/design/mechanical/blades/bladeless-turbine-converts-wind-into-fluid-power/

I want this redirected URL. How can i get this?

Upvotes: 4

Views: 13044

Answers (4)

J Shapiro
J Shapiro

Reputation: 3861

1) Specify that your class conforms to the UIWebViewDelegate protocol (and make sure your WebView's delegate outlet is connected to your view controller):

@interface YourWebsiteViewController : UIViewController <UIWebViewDelegate>

2) Add the following delegate method:

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    NSURL *url = [webView.request mainDocumentURL];
    NSLog(@"The Redirected URL is: %@", url);
}

Depending on what you're trying to do with this information, you might want to substitute #2 for this method (which will allow you the opportunity to prevent a page from loading):

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = [request mainDocumentURL];
    NSLog(@"The Redirected URL is: %@", url);
    // Return YES if you want to load the page, and NO if you don't.
    return NO;
}

Upvotes: 4

Roman
Roman

Reputation: 10403

That URL is part of the http header. This is what happening:

request 1: http://www.windpowerengineering.com/?p=11020

response 1: page has moved go to http://www.windpowerengineering.com/design/mechanical/blades/bladeless-turbine-converts-wind-into-fluid-power/

and then your browser will go:

request 2: http://www.windpowerengineering.com/design/mechanical/blades/bladeless-turbine-converts-wind-into-fluid-power/

response 3: here is the html

If you look at the http headers from response 1 you will be able to extract the url from the Location header. In Objective C this is one way to do it:

NSURLResponse* response = // the response, from somewhere
NSDictionary* headers = [(NSHTTPURLResponse *)response allHeaderFields];
NSString* redirection_url = [headers objectForKey:@"Location"];

In the above code you can access all of the response http headers from the headers dictionary.

Upvotes: 0

Ramaraj T
Ramaraj T

Reputation: 5230

Did you try this delegate method?

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

It will be called whenever the webview loads a webpage.

You can get the URL from one of this method's parameters, request

    NSURL *regURL=[request URL];
NSString *urlString=[regURL absoluteString];

Upvotes: 0

Shamis Shukoor
Shamis Shukoor

Reputation: 2515

use javascript

document.referrer

Upvotes: 0

Related Questions