簡體   English   中英

使用 C# 加載 web 站點的頁面源

[英]Loading page source of a web site using C#

我想獲取 web 站點的頁面源,該站點顯示敏感細節並且僅在我的辦公室筆記本電腦上運行。 它在幕后讀取我的用戶userIdpassword

在同一台筆記本電腦上,我有Visual Studio 2019 ,我使用這段代碼來完成任務;

      string url = "http://www.xyz/com/myreuests/get_form.aspx";

        using (HttpClient client = new HttpClient())
        {
            using (HttpResponseMessage response = client.GetAsync(url).Result)
            {
                using (HttpContent content = response.Content)
                {
                    string result = content.ReadAsStringAsync().Result;
                }
            }
        }

result我在HTML Tags中得到了這個:

 <div id="content"> <div class="content-container"> <h3>HTTP Error 401.2 - Unauthorized</h3> <h4>You are not authorized to view this page due to invalid authentication headers.</h4> </div> <div class="content-container"> <fieldset><h4>Most likely causes:</h4> <ul> <li>No authentication protocol (including anonymous) is selected in IIS.</li> <li>Only integrated authentication is enabled, and a client browser was used that does not support integrated authentication.</li> <li>Integrated authentication is enabled and the request was sent through a proxy that changed the authentication headers before they reach the Web server.</li> <li>The Web server is not configured for anonymous access and a required authorization header was not received.</li> <li>The "configuration/system.webServer/authorization" configuration section may be explicitly denying the user access.</li> </ul> </fieldset> </div> <div class="content-container"> <fieldset><h4>Things you can try:</h4> <ul> <li>Verify the authentication setting for the resource and then try requesting the resource using that authentication method.</li> <li>Verify that the client browser supports Integrated authentication.</li> <li>Verify that the request is not going through a proxy when Integrated authentication is used.</li> <li>Verify that the user is not explicitly denied access in the "configuration/system.webServer/authorization" configuration section.</li> <li>Create a tracing rule to track failed requests for this HTTP status code. For more information about creating a tracing rule for failed requests, click <a href="http://go.microsoft.com/fwlink/?LinkID=66439">here</a>. </li> </ul> </fieldset> </div>

我已經是活躍用戶了,有什么方法可以獲取這個 web 網站的頁面源代碼嗎?

我通過添加以下代碼解決了這個問題:

 WebClient client = new WebClient();
 client.Proxy = WebRequest.DefaultWebProxy;
 client.Credentials = System.Net.CredentialCache.DefaultCredentials; 
 client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM