我已经为网站植入了登录代码,之后我想从服务器下载文件。
HttpWebRequest http = WebRequest.Create(@"http://www.website.com") as HttpWebRequest;
// http.Connection = "Keep-alive"; //uncertain
http.Method = "POST";
http.ContentType = "application/x-www-form-urlencoded";
string postData = "FormNameForUserId=" + @"username" + "&FormNameForPassword=" + "pass";
byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
http.ContentLength = dataBytes.Length;
using (Stream postStream = http.GetRequestStream())
{
postStream.Write(dataBytes, 0, dataBytes.Length);
}
HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
// Probably want to inspect the http.Headers here first
http = WebRequest.Create(@"http://www.codeproject.com") as HttpWebRequest;
http.CookieContainer = new CookieContainer();
http.CookieContainer.Add(httpResponse.Cookies);
HttpWebResponse httpResponse2 = http.GetResponse() as HttpWebResponse;
我如何才能找到正确的信息,以及登录后我该怎么做才能下载文件?
答案 0 :(得分:0)
你有两个问题
你有没有谷歌这个?
注意:您说您已经实现了登录页面...最简单的部分是在登录完成后设置一个会话变量,并且每个页面都会重新检查该变量。
这是旧的方式,我们可以做更多的事情,但请提出您的问题,请继续使用:
if( txtUsername.Text == "user" && txtPassword.Text == "pwd" ) {
// Login correct
Session["Login-User"] = txtUsername.Text;
}
else {
// Login not correct, show error message and clear session
Session["Login-User"] = "";
}
然后在任何页面中,在开头(最好放在Page_Init
方法中)
If( Session["Login-user"] != null && Session["Login-user"].ToString().Length > 0 ) {
// Login OK
}
else {
// Login Not OK, redirect
Response.Redirect("~/Login.aspx");
}