为了实现自动提交表单,我在本地做了两个简单页面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>

这是为什么呢?

解决方案 »

  1.   

    应该往2.asp里面POSTstring uriString = "http://localhost/2.asp"; 
      

  2.   

    用request["userName"]这样获取信息、。
      

  3.   

    如果我按照上面的朋友说的
    “应该往2.asp里面POST 
    string uriString = "http://localhost/2.asp"; ”那么就会出现这样的问题srcString="用户名和密码是"
    而不是预想中的srcString="用户名和密码是user1password1"
    也就是说
    username=request.Form("username") 
    password=request.Form("password") 
    这些是没有获取到值的,整个表单提交也没有失败了。
      

  4.   

    http://www.cnblogs.com/anjou/archive/2006/12/25/602943.html