Winforms登录phpMyAdmin并获取HTML源代码

时间:2015-12-07 14:57:31

标签: c# winforms

在本地网络上,我有一个与此类似的页面:http://demo.phpmyadmin.net/master(此处使用:username =“root”,pass = blank)

在Winforms应用中,我需要传递登录名/密码并获取此HTML:http://demo.phpmyadmin.net/master/server_status.php?db=&token=fda5205792a3caf29517c97c59ab1599

我正在寻找3天的解决方案,但我无法通过登录部分。 这是我最好的。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        var client = new CookieAwareWebClient();
        client.BaseAddress = @"http://demo.phpmyadmin.net/master";
        var loginData = new NameValueCollection();
        loginData.Add("input_username", "root");
        //loginData.Add("password", "YourPassword");
        client.UploadValues(@"http://demo.phpmyadmin.net/master", "POST", loginData);

        string htmlSource = client.DownloadString("http://demo.phpmyadmin.net/master/server_status.php?db=&token=fda5205792a3caf29517c97c59ab1599");
        richTextBox1.Text = htmlSource;
    }
}
public class CookieAwareWebClient : WebClient
{
    private CookieContainer cookie = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).CookieContainer = cookie;
        }
        return request;
    }
}

0 个答案:

没有答案