我正在尝试编写一个C#程序,以延长我在大学图书馆的书籍的截止日期。我想做的是以下内容: 1.)通过WebRequest& amp;登录图书馆网站POST方法,用户名和&在C#程序中输入的密码 2.)获取包含加密密码&的“查看借来的图书”网站的网址。纯文本用户名为GET参数 3.)下载命名页面的内容以在C#程序中显示给用户 4.)如果用户按下程序中的相应按钮,请将延长表单提交到网站,以便立即延长所有媒体。
现在我被困在1和2之间,我似乎能够连接到网站并输入用户数据,但我得到的WebResponse再次是登录页面(如果你手动登录则不是这种情况)在网站上)。
这是我写的连接网站的方法:
// Login function, logs the user in, uses passed user number & password
public static Boolean userLogin(String unr, String pass)
{
// Login
// Cookie needed for maintaining php session
CookieContainer cContainer = new CookieContainer();
Console.WriteLine(unr+","+pass);
String postUrl = "https://universitylibrary.com/loan/DB=4/LNG=DU/USERINFO_LOGIN";
String formParams = String.Format("ACT={0}&HOST_NAME={1}&HOST_PORT={2}&HOST_SCRIPT={3}&LOGIN={4}&STATUS={5}&BOR_U={6}&BOR_PW={7}","UI_DATA","","","","KNOWNUSER","HML_OK", unr, pass);
String cookieHeader;
WebRequest wreq = WebRequest.Create(postUrl);
wreq.ContentType = "application/x-www-form-urlencoded";
wreq.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
wreq.ContentLength = bytes.Length;
using (Stream os = wreq.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = wreq.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
//Authentication trial
String PageSource;
String getUrl = "https://universitylibrary.com:443/loan/DB=4/USERINFO";
WebRequest getReq = WebRequest.Create(getUrl);
getReq.Headers.Add("Cookie",cookieHeader);
WebResponse getResp = getReq.GetResponse();
using (StreamReader sr = new StreamReader(getResp.GetResponseStream()))
{
PageSource = sr.ReadToEnd();
}
Console.Write(PageSource);
return true;
}
你能看出我的错误吗?我在控制台上获得了源代码和params(用户名,密码)输出,但输出又是登录页面。
我只看一下php页面,但我无法访问任何内部系统数据,我只有HTML页面。
任何建议都将受到高度赞赏!
编辑:
我重新思考了整个事情,并完全重建了fiddler记录的HTTP请求标头。该部分功能现在看起来像这样:
// Login
// Cookie needed for maintaining php session
CookieContainer cContainer = new CookieContainer();
HttpCookie cookie = new HttpCookie("cookie", "PSC_4='xxxxxxx'; DB='n'");
CookieCollection cookieCol = new CookieCollection();
cookieCol.Add(cookieCol);
cContainer.Add(cookieCol);
Console.WriteLine(unr+","+pass);
String postUrl = "https://universitylibrary.com:443/loan/DB=4/USERINFO";
String formParams = String.Format("ACT={0}&HOST_NAME={1}&HOST_PORT={2}&HOST_SCRIPT={3}&LOGIN={4}&STATUS={5}&BOR_U={6}&BOR_PW={7}","UI_DATA","","","","KNOWNUSER","HML_OK", unr, pass);
String cookieHeader;
HttpWebRequest wreq = (HttpWebRequest) WebRequest.Create(postUrl);
wreq.Referer = "https://universitylibrary.com/loan/DB=4/LNG=DU/USERINFO_LOGIN";
wreq.KeepAlive = true;
wreq.ContentLength = 119;
wreq.Host = "universitylibrary.com";
wreq.ContentType = "application/x-www-form-urlencoded";
wreq.Method = "POST";
wreq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0";
wreq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
wreq.SendChunked = true;
wreq.TransferEncoding = "gzip, deflate";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
wreq.ContentLength = bytes.Length;
using (Stream os = wreq.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
cookieHeader = "";
try
{
HttpWebResponse resp = (HttpWebResponse) wreq.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
}
catch (WebException ex)
{
Console.WriteLine(ex.Status);
Console.WriteLine(ex.Response);
}
但是整个事情仍然不起作用,和以前一样。 是否可能HttpWebRequest无法处理https或者其他内容缺少https才能工作? (HTTP& HTTPS似乎在语法上是相同的,端口正确设置为443,真正的区别似乎在于额外的SSL / TLS层,也许我需要在某处添加它?)
答案 0 :(得分:1)
当您返回登录页面时,可能意味着您未经过正确的身份验证。这可能有几个原因,但最终是因为您没有正确地通过网站上的手动登录来镜像HTTP通信。
我通常做的是使用Fiddler等监视器从手动登录中捕获完整的请求/响应模式,然后我可以在我的代码中镜像。
答案 1 :(得分:-1)
您可以在检查登录数据后修改登录页面,并使唯一的输出为“SuccessfullySignIn”。 如果这是您收到的数据, $ if(getReq ==“SuccessfullySignIn”) { //做一点事 } 并尝试在您的网页中使用重定向功能