我有一个获取用户名和密码的表单,提交后会显示该帐户的一些信息。形式是:
<form id="LoginForm" target="Window81525062.79020184" action="Login.aspx" method="post" name="LoginForm">
<input id="userID" type="text" maxlength="20" style="WIDTH: 160px; HEIGHT: 24px" onkeypress="CheckEnter()">
<input id="Password" type="password" maxlength="20" style="WIDTH: 160px; HEIGHT: 24px" onkeypress="CheckEnter()">
<div id="b2" class="social-media-shareTAK" style="width: 60px;">
<div class="inner"></div>
<ul>
<li>
<a title="enter" target="_blank">
<img src="/DL/Classes/BUTTON/images/Login.png" alt="" style="width: 38px; height: 38px; margin-top: 4px; cursor: pointer; position: absolute; left: 7px;">
</a>
</li>
</ul>
</div>
</form>
现在我想从一个数据库中输入大约100个帐户的用户名和密码,并将这些帐户的信息存储到另一个数据库中。我应该怎么做? (我更喜欢 C#,但如果有更好的方法请告诉我)谢谢 :)
最佳答案
这是一个 C# 示例(服务器端代码提供更好的控制,所以我推荐)
StringBuilder postData = new StringBuilder();
postData.Append("USERNAME_FIELD_NAME =" + HttpUtility.UrlEncode("USERNAME") + "&");
postData.Append("PASSWORD_FIELD_NAME =" + HttpUtility.UrlEncode("PASSWORD"));
// Now to Send Data.
StreamWriter writer = null;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(THE_URL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.ToString().Length;
try
{
writer = new StreamWriter(request.GetRequestStream());
writer.Write(postData.ToString());
HttpWebResponse WebResp = (HttpWebResponse)request.GetResponse();
//Now, we read the response (the string), and output it.
Stream Answer = WebResp.GetResponseStream();
StreamReader _Answer = new StreamReader(Answer);
Console.WriteLine(_Answer.ReadToEnd());
}
finally
{
if (writer != null)
writer.Close();
}
来源/方法:
http://msdn.microsoft.com/en-us/library/debx8sh9.aspx
http://forums.asp.net/t/1048041.aspx?How+to+programmatically+POST+data+to+an+aspx+page+
关于c# - 如何以编程方式发布 HTML 表单并获得响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22086895/