c#自动提交网页表单,并获取提交后的页面的内容
为了实现自动提交表单,我在本地做了两个简单页面1.asp和2.asp
1.asp代码如下:
<html>
<head>
</head>
<body>
<form action="2.asp" method="post" >
<input type="text" id="userName" name="userName"/>
<input type="text" id="password" name="password"/>
<input type="submit" />
</form>
</body>
</html>
2.asp代码如下:
<%
username=request.Form("username")
password=request.Form("password")
response.write "用户名和密码是"&username&password
%>
然后我写了个C# 小程序。
// 要提交表单的URI字符串。
string uriString = "http://localhost/1.asp";
// 要提交的字符串数据。
string postString = "userName=user1&password=password1";
// 初始化WebClient
WebClient webClient = new WebClient();
// webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
// 将字符串转换成字节数组
byte[] postData = Encoding.ASCII.GetBytes(postString);
// 上传数据,返回页面的字节数组
byte[] responseData = webClient.UploadData(uriString, "POST", postData);
// 返回的将字节数组转换成字符串(HTML)
string srcString = Encoding.UTF8.GetString(responseData);
可是为什么srcString 不是我预想中的接受提交信息的2.asp页面的内容“用户名和密码是user1password1”,而是1.asp的内容<html>
<head>
</head>
<body>
<form action="2.asp" method="post" >
<input type="text" id="userName" name="userName"/>
<input type="text" id="password" name="password"/>
<input type="submit" />
</form>
</body>
</html>
这是为什么呢?