简体   繁体   中英

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. It reads my userId and password behind the scenes.

On the same laptop, I have Visual Studio 2019 and I used this code to do the task;

      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 :

 <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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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