像C#一样在网页上发送POST请求吗?

时间:2011-06-16 19:19:19

标签: c# httpwebrequest

  

可能重复:
  How to simulate browser HTTP POST request and capture result in C#

我的网页上有以下表格......

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

我用这些来登录我的网站 但我也希望在我的应用程序中提供此功能 如何使用HTTP Web请求将用户和密码发送到我的验证页面?

3 个答案:

答案 0 :(得分: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 :(得分:2)

Scott Hanselman有一篇很棒的文章:

http://www.hanselman.com/blog/HTTPPOSTsAndHTTPGETsWithWebClientAndCAndFakingAPostBack.aspx

(非常类似于@The Mask响应,但在解释上有点冗长)

答案 2 :(得分:1)

相关问题