2

我想用我的程序(asp.net/C#)进入另一个站点。

  1. 我如何抓取网站并找到用户名和密码文本框并用所需的数据填充它们。
  2. 那么我如何按下/触发登录按钮?

注意:我之前使用 HtmlAgilityPack 抓取了网站。有没有办法使用这个 dll 或任何其他 dll?

例如,用户名是:用户密码是:密码,登录按钮 ID 是:按我该怎么做?

4

1 回答 1

3

你不想那样工作。您需要模拟一旦该表单通过 Post 请求提交到服务器并传递值后站点将执行的操作。您可以通过使用 Fiddler2 并正常执行表单提交来评估网站的功能,然后总结如下:

HttpWebRequest request;
        HttpWebResponse response;
        var responseData = "";
        var strUrl = "https://auctions.godaddy.com/trpSearchResults.aspx";

        var postData = string.Format("action=review_selected_add&items={0}_B_{1}_1|&rnd={2}&JlPYXTX=347bde7", auctionRef, bid,
            randomDouble(0, 1).ToString("0.00000000000000000"));
        request = (HttpWebRequest)WebRequest.Create(strUrl);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = postData.Length;
        request.Accept = "text/html, application/xhtml+xml, */*";
        request.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/x-silverlight, application/x-silverlight-2-b2, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*";
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11";
        request.Headers.Add("Accept-Encoding", "deflate");
        request.Referer = "auctions.godaddy.com";
        request.Headers["my-header"] = "the-value";
        request.KeepAlive = true;
        request.CookieContainer = cookies;
        request.Timeout = Timeout.Infinite;

        var stOut = new StreamWriter(request.GetRequestStream());
        stOut.Write(postData);
        stOut.Flush();
        stOut.Close();
        stOut = null;

        response = (HttpWebResponse)request.GetResponse();
        response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
        var encoding = new System.Text.UTF8Encoding();
        var responseReader = new StreamReader(response.GetResponseStream(), encoding, true);

        encoding = new System.Text.UTF8Encoding();
        responseReader = new StreamReader(response.GetResponseStream(), encoding, true);

        responseData = responseReader.ReadToEnd();
        response.Close();
        responseReader.Close();

只需使用上述方法创建一个类来发布数据并返回 html。

于 2013-01-25T11:15:09.557 回答