0

伙计们,我尝试使用以下方式登录http://waec2013.com/waecexam/网站

  1. HttpWebRequest
  2. 饼干容器

还有另一种技术我可以使用 webbrowser,但由于这是 web 应用程序,所以我不能使用 webbrowser。但是,我可以登录到该网站并获取特定数据是不是很幸运?

我进行逆向工程并进行一些编码,但没有达到我的结果。

有什么建议么

     string formUrl = "http://waec2013.com/waecexam/"; 
     string formParams = string.Format("adminName={0}&adminPass={1}&act={2}",
     "passwaec", "cee660","login");
     string cookieHeader;
     WebRequest req = WebRequest.Create(formUrl);
     req.ContentType = "application/x-www-form-urlencoded";
     req.Method = "POST";
     byte[] bytes = Encoding.ASCII.GetBytes(formParams);
     req.ContentLength = bytes.Length;
     using (Stream os = req.GetRequestStream())
     {
         os.Write(bytes, 0, bytes.Length);
     }
     WebResponse resp = req.GetResponse();
     cookieHeader = resp.Headers["Set-cookie"];

     string pageSource;
     string getUrl = "http://waec2013.com/waecexam/Leads.php";
     WebRequest getRequest = WebRequest.Create(getUrl);
     getRequest.Headers.Add("Cookie", cookieHeader);
     WebResponse getResponse = getRequest.GetResponse();
     using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
     {
        pageSource = sr.ReadToEnd();
    }
4

2 回答 2

1

最后,我已经解决了我自己的问题,并在需要时为你们发帖。

 public class CookiesAwareWebClient : WebClient
 {
    public CookieContainer CookieContainer { get; private set; }

    public CookiesAwareWebClient()
    {
        CookieContainer = new CookieContainer();
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
        var request = base.GetWebRequest(address);
        ((HttpWebRequest)request).CookieContainer = CookieContainer;
        return request;
    }
  }

   using (var client = new CookiesAwareWebClient())
    {
        var values = new NameValueCollection
        {
            { "adminName", "passwaec" },
            { "adminPass", "cee660" },
            { "x", "0" }, 
            { "y", "0" }, 
            { "act", "login" },
        };

        // We authenticate first
        client.UploadValues("http://waec2013.com/waecexam/index.php", values);

        // Now we can download
        client.DownloadFile("http://waec2013.com/waecexam/leadExp.php?act=export",
        @"c:\abc.txt");
      }
于 2013-02-12T11:08:56.473 回答
0

在方法的开头添加:

var cookies = new CookieContainer();

在创建 webrequest 的每一行之后,将 cookie 分配给实例化的请求:

WebRequest req = WebRequest.Create(formUrl);
req.CookieContainer = cookies;

这将存储所有传入的 cookie,并在 GET POST 时将容器中的所有 cookie 发送到网络服务器。

在这种情况下,您不需要使用 Set-Cookie 标头。

于 2013-02-12T07:33:51.273 回答