c# - WebResponse 不使用传入的 Cookie 进行身份验证
问题描述
我正在开发一个 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 值,但我没有看到它。
解决方案
推荐阅读
- c - 即使两个变量包含相同的字符串,sizeof() 也会返回不同的值
- kubernetes - 外部请求通过 Ingress 进入 k8s pod 的具体流程图是什么?
- java - 在 Java 运行时从数组中删除元素
- pyspark - org.apache.spark.SparkException:pyspark.daemon 的标准输出中没有端口号
- android - IPFS 从网络读取数据
- java - 处理必须在许多类中更改的导入
- python - PorterStemmer:在 Spyder 上运行时没有词干输出或错误
- angular - Angular 5:从拦截器中的 http 响应标头获取授权
- css - 如何获取一堆 scss 文件并为所有应用程序制作一个 css 文件
- javascript - 未捕获的类型错误:无法在“URL”上执行“createObjectURL”:未找到与提供的签名匹配的函数