我可以從網站上下載一個網頁,但如果頁面重定向,我怎麼能捕捉到新的地址。例如,如果我訪問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