2014-02-28 90 views
1

我有一個獲取用戶名和密碼的表單,提交後會顯示該帳戶的一些信息。形式是:如何以編程方式POST HTML表單並獲得回覆

<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#,但如果有更好的辦法,請讓我知道)謝謝:)

+1

你可以請清除你的需求..張貼表單在後臺使用jQuery/ajax是流行的方式..在最後一行你告訴你想要從一個數據庫插入100個帳戶的細節到另一個..所以它的不同任務..具體要求是什麼? –

+0

這個問題令人困惑,它聽起來像是想將帳戶詳細信息從一個數據庫遷移到另一個數據庫,不確定爲什麼要發佈代碼,或許您可以澄清並且我們可以嘗試爲您提供幫助。 – user65439

+0

是的,這是一箇舊數據庫,我們沒有直接訪問它,現在我們想改進它,我們需要它的數據。 – farshad22

回答

2

這裏是一個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(); 
     } 

來源/ HOWTO的:

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?

+0

還需要對用戶名和密碼字段的值進行編碼,例如,與'HttpUtility.UrlEncode'。 – harpo

+0

thankx,但它表示: 在調用[Begin] GetResponse之前,必須將ContentLength字節寫入請求流。 – farshad22

+0

@harpo ..正確,更新了答案 – LGSon

相關問題