xcode 如何获取重定向的 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13505889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:09:38  来源:igfitidea点击:

How to get Redirected URL

iphonehtmlobjective-ciosxcode

提问by user1673099

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.

我有一个 URL。当我尝试在浏览器中打开它时,它会重定向到另一个 URL 并显示内容。我想要那个内容但是我没有得到那个重定向的 URL。所以,我无法显示数据。

How can i do that programmatically??

我怎样才能以编程方式做到这一点?

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

例如,我拥有的 URL:http : //www.windpowerengineering.com/?p=11020

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

& 重定向的 URL 是:http : //www.windpowerengineering.com/design/mechanical/blades/bladeless-turbine-converts-wind-into-fluid-power/

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

我想要这个重定向的 URL。我怎样才能得到这个?

回答by J Shapiro

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

1) 指定您的类符合 UIWebViewDelegate 协议(并确保您的 WebView 的委托插座连接到您的视图控制器):

@interface YourWebsiteViewController : UIViewController <UIWebViewDelegate>

2) Add the following delegate method:

2)添加以下委托方法:

-(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):

根据您尝试使用此信息执行的操作,您可能希望用 #2 替换此方法(这将使您有机会阻止页面加载):

- (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;
}

回答by Shamis Shukoor

use javascript

使用 JavaScript

document.referrer

回答by Roman

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

该 URL 是 http 标头的一部分。这是正在发生的事情:

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

请求 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/

响应 1:页面已移至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/

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

response 3: here is the html

响应 3:这是 html

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

如果您查看响应 1 中的 http 标头,您将能够从Location标头中提取 url 。在 Objective C 中,这是一种方法:

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 headersdictionary.

在上面的代码中,您可以访问headers字典中的所有响应 http 标头。

回答by Ramaraj T

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.

每当 webview 加载网页时都会调用它。

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

您可以从该方法的参数之一中获取 URL, request

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