3

我有点卡在这里。我正忙于制作一个从网站读取数据的 Windows 应用程序。但是,de 网站需要先登录,而我似乎无法通过该登录。我对编程很陌生,所以我希望有人知道解决方案。

这是我用来登录的代码:

private void btnLogin2_Click(object sender, EventArgs e)
    {
        HttpWebRequest request = WebRequest.Create(LoginPageURL) as HttpWebRequest;
        request.KeepAlive = true;
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36";
        string postData = "j_username=" + number + "&j_password=" + password;
        byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
        request.ContentLength = dataBytes.Length;
        using (Stream postStream = request.GetRequestStream())
        {
            postStream.Write(dataBytes, 0, dataBytes.Length);
        }
        HttpWebResponse httpResponse = request.GetResponse() as HttpWebResponse;
        request = WebRequest.Create(Page2URL) as HttpWebRequest;
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36";
        request.CookieContainer = new CookieContainer();
        request.CookieContainer.Add(httpResponse.Cookies);
        request.Method = "GET";
        HttpWebResponse httpResponse2 = request.GetResponse() as HttpWebResponse;
        StreamReader stream = new StreamReader(httpResponse2.GetResponseStream(), System.Text.Encoding.UTF8);
        string result = stream.ReadToEnd();
        stream.Close();

        tbOutput2.Text = result;



    }

关键是我在登录后获得了页面的 HTML(Page2URL)。但我不断从登录页面获取 HTML。

4

1 回答 1

0

您正在将 cookie 添加到来自第一个响应的请求中:

request.CookieContainer.Add(httpResponse.Cookies);

响应的 cookie 可能为空!为了解决这个问题,从响应头中读取 cookie 值并将它们添加到下一个请求中,如下所示:

 string response_header_cookies = response.Headers.Get("Set-Cookie")    
 req.Headers.Add("Cookie",response_header_cookies); 

在大多数情况下,这是更有效的方法。希望这可以帮助!来源:msdn

于 2014-02-10T16:00:07.130 回答