2011-05-20 116 views
0

我已經植入了一個登錄代碼到一個網站,然後我想從服務器上下載一個文件。登錄網站並下載文件?

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

你有兩個問題

  • 登錄
  • 下載文件

你有沒有谷歌嗎?

注意:你說你已經實現了一個登錄頁面...最容易的部分被設置會話變量登錄完成後併爲每個頁面重新檢查該變量。

這是老樣子,有很多更多我們所能做的,而是形成你的問題,請與本:

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"); 
} 

Learn more in ASP.NET official website