简体   繁体   English

使用 C# 加载 web 站点的页面源

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

I want to get the page source of a web site which shows sensitive details and run only on my office laptop.我想获取 web 站点的页面源,该站点显示敏感细节并且仅在我的办公室笔记本电脑上运行。 It reads my userId and password behind the scenes.它在幕后读取我的用户userIdpassword

On the same laptop, I have Visual Studio 2019 and I used this code to do the task;在同一台笔记本电脑上,我有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;
                }
            }
        }

And in result I am getting this in HTML Tags : 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>

I am already active user so is there any way get page source for this web site?我已经是活跃用户了,有什么方法可以获取这个 web 网站的页面源代码吗?

I resolved the issue by adding this code:我通过添加以下代码解决了这个问题:

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

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM