2011-11-29 37 views
1

我發現this great class發送POST請求到一個網頁。現在我在單個表單的測試頁上嘗試了它,它可以工作。但現在我想對頁面執行POST請求,該頁面在同一頁面上有多個表單。如果使用名稱定義,是否可以選擇特定的表單?發送POST請求到多個表單的頁面

例如,像這樣:

<form method="post" action="index.php" target="_parent"> 
... 
</form> 
<form method="post" action="index.php" name="login" target="_top" class="login"> 
// This is the form that I want to post data 
... 
<input value="Go" type="submit" id="input_go" /> 
</form> 

編輯:

由Anfurny提供此解決方案不起作用:

post.PostItems.Add("login", "input_go"); 

我已經更新代碼,即提交按鈕只有id被定義。

EDIT2:

我發現了一個網頁的一個很好的例子有兩種形式。 phpMyAdmin demo,它有兩種形式,一種是用於選擇不同的語言,另一種是用於登錄。該演示有:

  • 用戶名=根
  • 密碼爲空

現在我試圖用這個代碼編程方式登錄(它使用了我已張貼在頁面開始鏈接類):

// First we create an instance of a class PostSubmitter. 
PostSubmitter post = new PostSubmitter(); 

// We set the URL to which the POST should go. 
post.Url = "http://demo.phpmyadmin.net/STABLE/index.php"; 

// We add items (password and username). 
post.PostItems.Add("pma_username", "root"); 
post.PostItems.Add("pma_password", ""); 

// Also tried to add to which login form should post and the 
// value set to id of the submit button, no luck. 
//post.PostItems.Add("login_form", "input_go"); 

// Here we set the method. 
post.Type = PostSubmitter.PostTypeEnum.Post; 

// Getting back the result. 
string result = post.Post(); 

// Writting it to the file. 
TextWriter tw = new StreamWriter("C:/response.txt"); 
tw.WriteLine(result); 
tw.Close(); 

response.txt文件具有與登錄頁面相同的內容,它應具有歡迎頁面的代碼。現在我該如何更改代碼,才能成功登錄?

回答

0

通常當你有多個表單發佈到同一個地方時,你會用被點擊的按鈕來區分它們。你不能做同樣的事情,並添加一個虛擬按鈕(動作)到你的後期價值列表?

+0

沒有,不起作用,因爲我已經在上面的評論中解釋過了。 –

+0

奇怪。您已將所有Request.Form值記錄到記錄器或文件以查看正在經歷的內容?抱歉,我對你的按鈕問題有點困惑。我不認爲你的問題中的實際表單與C#表單發佈類的內容有什麼關係。如果出於某種奇怪的原因,它會。爲該按鈕添加一個NAME屬性。 Id不會發布。 –

+0

無法更改頁面的內容,因此無法向該按鈕添加名稱屬性。我已經將返回的字符串打印到txt文件中,它實際上返回頁面的源代碼,但它應該返回帶有錯誤附帶的源代碼(插入的數據不正確)。所以看起來請求沒有被正確發送。 –

0

您是否嘗試過做這樣的事情:

post.PostItems.Add("FORM_NAME","SUBMIT_BUTTON_NAME "); 

其中FORM_NAME是您希望提交的表單的名稱,而SUBMIT_BUTTON_NAME是您希望假裝按下的提交按鈕的名稱(如果存在多個)。

+0

我試過這個,但沒有成功。輸入定義如下:。它沒有名字,但它有id,所以我嘗試了這個:post.PostItems.Add(「login」,「input_go」)。但正如我所說,它沒有奏效。 –