Getting HTML from a page behind a login

前端 未结 8 1996
甜味超标
甜味超标 2021-01-16 05:59

This question is a follow up to my previous question about getting the HTML from an ASPX page. I decided to try using the webclient object, but the problem is that I get the

相关标签:
8条回答
  • 2021-01-16 06:18

    As the aspx page I was trying to get was in my own projct, I could use the Server.Execute method. More details in my answer to my original question

    0 讨论(0)
  • 2021-01-16 06:28

    Just pass valid login parameters to a given URI. Should help you out.

    If you don't have login information you shouldn't be trying to circumvent it.

    public static string HttpPost( string URI, string Parameters )
          {
             System.Net.WebRequest req = System.Net.WebRequest.Create( URI );
             req.ContentType = "application/x-www-form-urlencoded";
             req.Method = "POST";
             byte[] bytes = System.Text.Encoding.ASCII.GetBytes( Parameters );
             req.ContentLength = bytes.Length;
             System.IO.Stream os = req.GetRequestStream();
             os.Write( bytes, 0, bytes.Length );
             os.Close();
             System.Net.WebResponse resp = req.GetResponse();
             if ( resp == null ) return null;
             System.IO.StreamReader sr = new System.IO.StreamReader( resp.GetResponseStream() );
             return sr.ReadToEnd().Trim();
          }
    
    0 讨论(0)
  • 2021-01-16 06:33

    Try setting the credentials property of the WebClient object

    WebClient ww = new WebClient();
    ww.Credentials = CredentialCache.DefaultCredentials;
    ww.DownloadString("Login.aspx?UserName=&Password=");
    string html = ww.DownloadString("Internal.aspx");
    
    0 讨论(0)
  • 2021-01-16 06:35

    @Fire Lancer: I asked myself that same question during my tests, so I checked, and it does work from a browser.

    0 讨论(0)
  • 2021-01-16 06:37

    Use Firefox with the LiveHttpHeaders plugin.
    This will allow you to login via an actual browser and see EXACTLY what is being sent to the server. My first question would be to verify that it isn't expecting a POST from the form. The example URL you are loading is sending the info via a querystring GET.

    0 讨论(0)
  • 2021-01-16 06:40

    Use Fiddler to see the HTTP requests and responses that happen when you do it manually through the browser.

    0 讨论(0)
提交回复
热议问题