2012-08-01 84 views
0
  1. 登錄,並作出新的(餅乾)會議(systempage.local/login.php中)
  2. 如果登錄成功定位到頁面,並做一些事情(導航到systempage.local/index.php文件)

我正在嘗試編寫本地網絡中某個站點的應用程序。
login.php通過usral重定向到index.php。 (在瀏覽器中)
我有我的webrequest準備登錄到它。 (它的工作原理,到目前爲止)HTTPWebRequest登錄。登錄後瀏覽

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using System.Net; 
using System.IO; 

namespace WindowsFormsApplication1 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    string sThepage; 
    CookieContainer logincookie; 

    private void button1_Click(object sender, EventArgs e) 
    { 
     string strPostData = "user_name=" + txtUser.Text + "&password=" + txtPass.Text + "&language=en&action%3Asubmit=Submit"; 
     CookieContainer tempCookies = new CookieContainer(); 

     ASCIIEncoding encoding = new ASCIIEncoding(); 
     byte[] data = encoding.GetBytes(strPostData); 

     HttpWebRequest postReq = (HttpWebRequest)WebRequest.Create("system.local/Login.php"); 
     postReq.Method = "POST"; 
     postReq.KeepAlive = true; 
     postReq.AllowAutoRedirect = false; 
     postReq.Accept = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; 

     postReq.ContentType = "application/x-www-form-urlencoded"; 
     postReq.Referer = "system.local/interface/Login.php"; 
     postReq.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1"; 
     postReq.ContentLength = data.Length; 

     Stream postreqStream = postReq.GetRequestStream(); 
     postreqStream.Write(data, 0, data.Length); 
     postreqStream.Close(); 

     HttpWebResponse postResponse; 
     postResponse = (HttpWebResponse)postReq.GetResponse(); 
     tempCookies.Add(postResponse.Cookies); 
     logincookie = tempCookies; 

     StreamReader postRegReader = new StreamReader(postResponse.GetResponseStream()); 
     sThepage = postRegReader.ReadToEnd(); 

     // Blanksite if login success. 
     if (!sThepage.Contains("!doctype")) 
     { 
      MessageBox.Show("you logged in"); 
      Navigate(); //Navigate to the index.php 
     } 
     else 
     { 
      MessageBox.Show("Client was unable to connect!");  

     } 

     richTextBox1.Text = sThepage; 
    } 
} 
} 

..

現在我需要一種方法來導航到indexpage(以及futher一些其他網頁)

private void Navigate() 
{ 
    // What to do here? 
    // and how to keep my cookie(sesion) for the next page? 
} 

回答

0

你所想必須做的是在登錄請求完成之後,將發送給變量/文件的cookie記錄下來,並將這些cookie再次發送回每個後續請求。您可以從標題對象中獲取Cookie。

+0

可變 'WebHeaderCollection myHeader;' ... '//第一請求(登錄) myHeader = postResponse.Headers; ...' 我應該在哪裏添加標題? (newReg.cookiecontainer或header?) 'HttpWebRequest newReq =(HttpWebRequest)WebRequest.Create(「system.local/interface/index.php」); newReq.Headers = myHeader;' ....對不起,格式化不起作用,希望你能理解它。 :/ – MrMAG 2012-08-01 10:13:20

+0

主要問題是我應該在哪裏添加標頭/ cookie? 'newReg。??? = myHeader;'...希望現在更容易理解:) – MrMAG 2012-08-01 10:23:20

+0

參見http://stackoverflow.com/questions/4158448/c-sharp-webrequest-using-cookies – PhonicUK 2012-08-01 10:31:24