c# - 使用C#将数据HttpPost到网页

标签 c# post httpwebrequest

我想使用 C# 代码登录网站。

这是示例表单的 html 代码:

<form action="http://www.site.com/login.php" method="post" name="login" id="login">
<table border="0" cellpadding="2" cellspacing="0">
<tbody>
<tr><td><b>User:</b></td><td colspan=\"2\"><b>Password:</b></td></tr>
<tr>
<td><input class="inputbg" name="user" type="text"></td>
<td><input class="inputbg" name="password" type="password"></td>
<td><input type="submit" name="user_control" value="Submit" class="buttonbg"></td>
</tr>
</tbody></table>
</form>

这是我迄今为止尝试过但没有成功的结果:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.site.com/login.php");
request.Method = "POST";
using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
{
    writer.Write("user=user&password=pass&user_control=Eingabe");
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    stream = new StreamWriter("login.html");
    stream.Write(reader.ReadToEnd());
    stream.Close();
}

任何想法,为什么会失败?

最佳答案

查看cookiecontainer .

使用基于表单的身份验证的网站通常依赖于 cookie。如果不设置请求的 cookiecontainer,它将不支持 cookie。

关于c# - 使用C#将数据HttpPost到网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2799368/

相关文章:

c# - 在 C# 中迭代​​数据行

c# - 具有有状态事件参数的异步事件处理程序

c# - 发送 multipart/form-data 内容类型请求

java - Spring 启动: Posting to a REST API is storing a single string as an array

java - 监听 HTTP 请求

c# - Visual Studio : How to properly build and specify the configurations and platforms for x64 and x86

c# - Roslyn 的 ".WithFooToken()"调用是否多余?

spring - 如何使用 Spring Data REST 发布嵌套实体

c# - 有人可以将 C# "HttpWebRequest"转换为 VB6 吗?

c# - 使用 HttpWebRequest.GetResponse() 抓取屏幕时出现错误 "The remote server returned an error: (403) Forbidden"