GetResponse() 的 StatusCode 正常,但 url 重定向
StatusCode for GetResponse() is OK but url Redirect
执行以下代码行时:
string link = "https://finance.yahoo.com/r/4951f719-c8e1-3b1d-b4db-684ef6739b8e/iphone-x-5-reasons-to-wait?utm_source=yahoo&utm_medium=partner&utm_campaign=yahootix&partner=yahootix&yptr=yahoo&.tsrc=rss"
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(link);
myHttpWebRequest.MaximumAutomaticRedirections=100;
myHttpWebRequest.AllowAutoRedirect=true;
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
我得到 StatusCode "OK" 并且 ResponseUri 仍然是原来的 URL 而不是重定向的。
如何获取最后一次重定向URL?
AllowAutoRedirect
属性 仅在 URL 被 HTTP 协议重定向时有效(当服务器 returns HTTP 代码 30x 时)。但是这个 URL 被包含在 HTML 页面中的 javascript (或 meta http-equiv='refresh'
标签,如果浏览器不支持 javascript )重定向。因此,您必须解析 HTML 页面内容并从中读取 URL。以下代码使用 HtmlAgilityPack 库(作为 NuGet 包提供)解析 HTML 页面并读取所需的 URL:
string link = "https://finance.yahoo.com/r/4951f719-c8e1-3b1d-b4db-684ef6739b8e/iphone-x-5-reasons-to-wait?utm_source=yahoo&utm_medium=partner&utm_campaign=yahootix&partner=yahootix&yptr=yahoo&.tsrc=rss";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(link);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
using (var responseStream = myHttpWebResponse.GetResponseStream())
{
using (var sr = new StreamReader(responseStream))
{
//content of HTML page
var html = sr.ReadToEnd();
//using HTMLAgilityPack library to parse HTML page
var htmlDocument = new HtmlAgilityPack.HtmlDocument();
htmlDocument.LoadHtml(html);
//find URL in HTML page. Null-checks omitted for simplicity
var contentAttribute = htmlDocument.DocumentNode.SelectSingleNode("noscript/meta").Attributes["content"].Value;
var URL = contentAttribute.Split(';')[1];//skip "0;" in contentAttribute
URL = URL.Substring(4);//skip 'URL='
URL = URL.Trim('\'');//remove surrounding '
}
}
执行以下代码行时:
string link = "https://finance.yahoo.com/r/4951f719-c8e1-3b1d-b4db-684ef6739b8e/iphone-x-5-reasons-to-wait?utm_source=yahoo&utm_medium=partner&utm_campaign=yahootix&partner=yahootix&yptr=yahoo&.tsrc=rss"
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(link);
myHttpWebRequest.MaximumAutomaticRedirections=100;
myHttpWebRequest.AllowAutoRedirect=true;
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
我得到 StatusCode "OK" 并且 ResponseUri 仍然是原来的 URL 而不是重定向的。
如何获取最后一次重定向URL?
AllowAutoRedirect
属性 仅在 URL 被 HTTP 协议重定向时有效(当服务器 returns HTTP 代码 30x 时)。但是这个 URL 被包含在 HTML 页面中的 javascript (或 meta http-equiv='refresh'
标签,如果浏览器不支持 javascript )重定向。因此,您必须解析 HTML 页面内容并从中读取 URL。以下代码使用 HtmlAgilityPack 库(作为 NuGet 包提供)解析 HTML 页面并读取所需的 URL:
string link = "https://finance.yahoo.com/r/4951f719-c8e1-3b1d-b4db-684ef6739b8e/iphone-x-5-reasons-to-wait?utm_source=yahoo&utm_medium=partner&utm_campaign=yahootix&partner=yahootix&yptr=yahoo&.tsrc=rss";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(link);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
using (var responseStream = myHttpWebResponse.GetResponseStream())
{
using (var sr = new StreamReader(responseStream))
{
//content of HTML page
var html = sr.ReadToEnd();
//using HTMLAgilityPack library to parse HTML page
var htmlDocument = new HtmlAgilityPack.HtmlDocument();
htmlDocument.LoadHtml(html);
//find URL in HTML page. Null-checks omitted for simplicity
var contentAttribute = htmlDocument.DocumentNode.SelectSingleNode("noscript/meta").Attributes["content"].Value;
var URL = contentAttribute.Split(';')[1];//skip "0;" in contentAttribute
URL = URL.Substring(4);//skip 'URL='
URL = URL.Trim('\'');//remove surrounding '
}
}