0

我想废弃一个在登录时显示成员电子邮件的网页,使用两个 Webbrowser 控件,我能够使用一个用于登录,另一个 Web 浏览器用于我所需的页面。

因为我有 1000 个 url,所以我想使用 http 请求和响应并使用 regax 表达式来获得所需的输出。

无论如何,http请求是否会记住登录名并显示所有成员电子邮件?

4

1 回答 1

0

这很简单,使用 WebBrowser 控件登录,完成后将 cookie 信息保存在容器中,如下所示:

           string[] array = webBrowser.Document.Cookie.Split(new char[]
                        {
                            ';'
                        });
                        for (int i = 0; i < array.Length; i++)
                        {
                            string cookie = array[i];
                            string name = cookie.Split(new char[]
                            {
                                '='
                            })[0];
                            string value = cookie.Substring(name.Length + 1);
                            string path = "/";
                            string domain = "abc.com";
                            yummycookies.Add(new Cookie(name.Trim(), value.Trim(), path, domain));
                        }

现在您在容器中拥有 cookie,现在将 HttpWebRequest 与此 cookie 容器一起使用,就像您已经登录一样,因为您拥有登录 cookie。

 public  string getHtml(string url)
    {
        string responseData = "";
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Accept = "*/*";
            request.AllowAutoRedirect = true;
            request.UserAgent = "http_requester/0.1";
            request.Timeout = 60000;
            request.Method = "GET";
            request.CookieContainer=yummycookies;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream responseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(responseStream);
                responseData = myStreamReader.ReadToEnd();
            }
            response.Close();
        }
        catch (Exception e)
        {
            responseData = "An error occurred: " + e.Message;
        }
        return responseData;
    }
于 2013-02-15T21:18:54.500 回答