【问题标题】:How to POST HTML form programmatically and get response如何以编程方式发布 HTML 表单并获得响应
【发布时间】:2014-02-28 05:21:05
【问题描述】:

我有一个获取用户名和密码的表单,提交后会显示该帐户的一些信息。形式是:

<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#,但如果有更好的方法请告诉我)谢谢 :)

【问题讨论】:

  • 能否请您清除您的要求.. 使用 jquery /ajax 在后台发布表单是一种流行的方式.. 在最后一行中,您告诉您要将 100 个帐户详细信息从一个数据库插入另一个数据库。 . 所以它的任务不同.. 确切的要求是什么?
  • 这个问题令人困惑,听起来您想将帐户详细信息从一个数据库迁移到另一个数据库,不确定为什么要发布 from 代码,也许您可​​以澄清一下,我们可以尝试为您提供帮助。
  • 是的,这是一个旧数据库,我们无法直接访问它,现在我们想要改进它,我们需要它的数据。

标签: c# javascript html


【解决方案1】:

这是一个 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+

How to simulate HTTP POST programatically in ASP.NET?

【讨论】:

  • 用户名和密码字段的值也需要编码,例如HttpUtility.UrlEncode.
  • thankx,但它说:您必须在调用 [Begin]GetResponse 之前将 ContentLength 字节写入请求流。
  • @farshad22 .. 答案已更新为其他示例/方式的多个来源.. 让您选择 :)
  • 我可以通过回答你的问题来帮助你如何做和提供样本,但我不能做这项工作或教如何理解。如果你需要这方面的帮助,你需要雇人。
猜你喜欢
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-15
相关资源
最近更新 更多