我可以从网站上下载一个网页,但如果页面重定向,我怎么能捕捉到新的地址。例如,如果我访问google.com网站,我想抓取它重定向到的页面,以便我可以获取ei代码。如何获得使用</p> <pre><code>sString = new System.Net.WebClient().DownloadString(Page); </code></pre> <p>在C#中的重定向地址
1
A
回答
1
您需要检查HTTP响应中包含的HTTP状态,如果它是HTTP“302 Found”,则需要从响应中获取“位置”标头的值。该值将成为重定向的目标,因此您需要下载目标。
String content;
try
{
content = new System.Net.WebClient().DownloadString(page);
}
catch(WebException e)
{
HttpWebResponse response = (System.Net.HttpWebResponse)we.Response;
... examine status, get headers, etc ...
}
0
下面是它是如何使用的HttpClient
string Page = "https://stackoverflow.com/questions/44980231/";
HttpClientHandler ClientHandler = new HttpClientHandler();
ClientHandler.AllowAutoRedirect = false;
HttpClient client = new HttpClient(ClientHandler);
HttpResponseMessage response = await client.GetAsync(Page);
try
{
string location = response.Headers.GetValues("Location").FirstOrDefault();
if (!Uri.IsWellFormedUriString(location, UriKind.Absolute))
{
Uri PageUri = new Uri(Page);
location = PageUri.Scheme + "://" + PageUri.Host + location;
}
MessageBox.Show(location);
}
catch
{
MessageBox.Show("No redirect!");
}
结果做:
相关问题
- 1. </p> <pre><code>console.log(new Buffer(12)) </code></pre> <p>如何使用的console.log
- 2. 使用>>,>,> |,||,| <,<,<<,<<
- 3. 使用<<< CON在PHP中
- 4. operator <<:std :: cout << i <<(i << 1);
- 5. Overloading << C++
- 6. C++重载<< error
- 7. C++重载<< operator
- 8. 重载operator << - C++
- 9. 什么意思的符号在java中“<<<”“<< =”“<<”
- 10. 如何重载operator <<?
- 11. << <<超载
- 12. 在C++中重载<<运算符
- 13. 如何在PHP中使用该“<<< HTML”的东西?
- 14. 在bash中,<<<用于什么?
- 15. 运营商<<在C#
- 16. 的Rails搞乱我的代码以“<<<<<<< HEAD”
- 17. 我如何使用<option></option><sup></sup>在HTML中
- 18. 在<和<=
- 19. PHP使用$ var = <<< EOM
- 20. 重载运算符<<在C++
- 21. 制作一个std :: funtion如果我有两个功能</p> <pre><code>void foo() { std::cout << 1 << std::endl; } void bar() { std::cout << 2 << std::endl; } </code></pre> <p>指向两个函数C++
- 22. <%$,<%@,<%=,<%#...这是怎么回事?
- 23. Asp.net:使用的<%#或<%
- 24. Map <String,Map <String,Boolean >> myMap = new HashMap <String,HashMap <String,Boolean >>();
- 25. 如何使用的cout << MyClass的
- 26. 使用<<运算符
- 27. 如何使用</p> <pre><code>ModelMetadata.FromLambdaExpression(expression, html.ViewData) </code></pre> <p>然而
- 28. C++重载错误<<使用向量
- 29. 的MySQL/PHP:<code>id</code>,<code>name</code>,<code>category</code>和一个<code>ratings</code>表列:<code>id</code>,<code>item</code>,<code>rating</code>如何使用JOIN语句
- 30. PHP操作<<<
这我知道,但我不知道该怎么办。 – MiscellaneousUser