首页 > 解决方案 > 使用 C# 加载网站的页面源

问题描述

我想获取一个显示敏感细节并仅在我的办公室笔记本电脑上运行的网站的页面源。它读我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>

我已经是活跃用户了,有什么方法可以获取该网站的页面源代码吗?

标签: c#

解决方案


我通过添加以下代码解决了这个问题:

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

谢谢


推荐阅读