2011-06-16 58 views
1

可能重複:
How to simulate browser HTTP POST request and capture result in C#用C#發送POST請求就像網頁一樣嗎?

我在我的網頁下面的表格......

<input type="text" tabindex="1" id="username" name="username" size="30"/> 
<input type="password" tabindex="2" id="password" name="password" size="30"/> 

我使用這些登錄到我的網站。
但我想在我的應用程序中提供此功能。
如何使用HTTP Web請求將用戶和密碼發送到我的驗證頁面?

回答

8

嘗試使用WebClient類。 例如:

var url = @"..."; 
var nvc = new System.Collections.Specialized.NameValueCollection(); 
nvc.Add("username", "username"); 
nvc.Add("password", "password"); 
var client = new System.Net.WebClient(); 
var data = client.UploadValues(url, nvc); 
var res = System.Text.Encoding.ASCII.GetString(data); 
Console.WriteLine(res); 
Console.ReadLine(); 
+1

這看起來更健全。我會去嘗試一下。 – Vercas 2011-06-16 19:45:59