WebResponse不使用传入的Cookie进行身份验证

时间:2019-05-01 03:26:54

标签: c# cookies system.net.httpwebrequest

我正在开发C#控制台应用程序以从PHP网站提取数据。该网站具有公共视图和扩展视图,可供登录用户使用。我可以使用我的凭据,用户名和密码通过FireFox,Edge和ID登录到站点,并成功查看其他材料。解析公共数据不是问题,但问题在于扩展数据的提取。

为了访问扩展视图,我尝试使用各种方法根据我的登录凭据创建cookie。我的逻辑背后是这样的:有一个使用用户名和密码的登录php页面。成功登录后,如果您已登录,则随后对公用页面的每次访问都会显示其他信息(扩展数据)。

我正在尝试将我的凭据作为httpwebrequest中cookie容器的一部分传递,或者将我现有的cookie从firefox(登录后)复制到我的httpwebrequest的cookie容器中。之后,我将使用HttpWebResponse对象查看返回的数据流。在这里,我应该能够识别html中的扩展数据。但是,据我所知,它只是返回了公众视野。

我能够成功打开firefox cookie,但不知道它们是否已在CookieContainer中成功使用

//!!!!Firefox cookie method!!!!//

CookieContainer ffCookieContainer = new CookieContainer();
Cookie ffCookie = new Cookie();
bool fRtn = false;
string strPath, strTemp, strDb, strHost, strField, Value;

strPath = @"C:\Documents and Settings\YourUserName\Application Data\Mozilla\Firefox\Profiles\urq2tlhr.default\cookies.sqlite";
strDb = "Data Source=" + strPath;

// Now open the temporary cookie jar and extract Value from the cookie if we find it.
using (SqliteConnection conn = new SqliteConnection())
{
    using (SqliteCommand cmd = conn.CreateCommand())
    {
        cmd.CommandText = "SELECT host, name, value, path, expiry, isSecure, isHttpOnly, lastAccessed, id FROM moz_cookies;"; 
        conn.ConnectionString = strDb;
        conn.Open();
        using (SqliteDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                Value = reader.GetString(0) + " " + reader.GetString(1) + " " + reader.GetString(2) + " " + reader.GetString(3) + " " + reader.GetString(4) + " " + reader.GetString(5) + " " + reader.GetString(6) + " " + reader.GetString(7) + " " + reader.GetString(8);
                if (!Value.Equals(string.Empty))
                {
                    //fRtn = true;
                    //break;
                    try
                    {
                        ffCookie.Discard = false;
                        ffCookie.Expired = false;
                        ffCookie.Domain = reader.GetString(0);
                        ffCookie.Expires = Convert.ToDateTime("01/01/2025");
                        ffCookie.Domain = reader.GetString(0);
                        ffCookie.HttpOnly = false;
                        ffCookie.Secure = false;
                        ffCookie.Port = "";
                        ffCookie.Name = reader.GetString(1);
                        ffCookie.Path = reader.GetString(3);
                        ffCookie.Value = reader.GetString(2);
                        Console.WriteLine(Value);
                        ffCookieContainer.Add(ffCookie);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
        conn.Close();
    }
}

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("website/view");
request.CookieContainer = ffCookieContainer;

//SECOND METHOD creating cookies based on credentials
string parameters = "username=blah@domain.com&password=blah";
string userName = "blah@domain.com";
string password = "blah";
string url = "example.com/index.php?forcelogin=1";
HttpWebRequest requestTwo = (HttpWebRequest)WebRequest.Create(url);
requestTwo.Method = "POST";
requestTwo.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
requestTwo.Headers.Add("Accept-Encoding: gzip,deflate");
requestTwo.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
requestTwo.KeepAlive = true;
requestTwo.Headers.Add("Keep-Alive: 900");
requestTwo.Referer = "example.com/index.php?forcelogin=1";
requestTwo.ContentLength = parameters.Length;
requestTwo.ContentType = "application/x-www-form-urlencoded";
requestTwo.CookieContainer = new CookieContainer();

using (Stream stream = requestTwo.GetRequestStream())
{
    byte[] paramAsBytes = Encoding.Default.GetBytes(parameters);
    stream.Write(paramAsBytes, 0, paramAsBytes.Count());
}
CookieContainer myContainer = new CookieContainer();

using (HttpWebResponse responseTwo = (HttpWebResponse)requestTwo.GetResponse())
{
    foreach (var cookie in responseTwo.Cookies)
    {
        var properties = cookie.GetType()
                               .GetProperties()
                               .Select(p => new
                               {
                                   Name = p.Name,
                                   Value = p.GetValue(cookie)
                               });

        foreach (var property in properties)
        {
            Console.WriteLine("{0}: {1}", property.Name, property.Value);
        }
    }
    myContainer = requestTwo.CookieContainer;
}
//repeat the cookie reassignment to the web request for the public view of the web page

//READ THE RESPONSE
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
    Stream receiveStream = response.GetResponseStream();
    StreamReader readStream = null;
    if (response.CharacterSet == null)
        readStream = new StreamReader(receiveStream);
    else
        readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
    string data = readStream.ReadToEnd();
    response.Close();
    readStream.Close();
}

我希望能够在页面中看到一个新生成的html值,但我没有看到它。

0 个答案:

没有答案