2011-05-12 107 views
0

有一個網站我需要使用HttpRequest登錄。該網站的登錄表單使用POST方法。我知道如何將HttpRequest用於沒有保護的頁面,我如何使用POST登錄到網站?使用POST和HttpRequest登錄網站

+0

讓我們看看你已經嘗試過什麼。 – 2011-05-12 14:44:57

回答

1

這個例子是由http://www.netomatix.com提供的。 POST設置爲代碼後面的OnClick處理程序中的HttpWebRequest.Method屬性。

實例形式:

<form name="_xclick" target="paypal" 
    action="https://www.paypal.com/cgi-bin/webscr" method="post"> 
    <input type="hidden" name="cmd" value="_cart"> 
    <input type="hidden" name="business" value="[email protected]"> 
    <input type="hidden" name="item_name" value="HTML book"> 
    <input type="hidden" name="amount" value="24.99"> 
    <input type="image" src="http://www.paypal.com/images/sc-but-01.gif" 
     border="0" name="submit" alt="Make payments with PayPal!"> 
    <input type="hidden" name="add" value="1"> 
</form> 

後面的代碼:

private void OnPostInfoClick(object sender, System.EventArgs e) 
{ 
    string strId = UserId_TextBox.Text; 
    string strName = Name_TextBox.Text; 

    ASCIIEncoding encoding=new ASCIIEncoding(); 
    string postData="userid="+strId; 
    postData += ("&username="+strName); 
    byte[] data = encoding.GetBytes(postData); 

    // Prepare web request... 
    HttpWebRequest myRequest = 
     (HttpWebRequest)WebRequest.Create("http://localhost/MyIdentity/Default.aspx"); 


    myRequest.Method = "POST"; // <<--- This is the key word of the day 


    myRequest.ContentType="application/x-www-form-urlencoded"; 
    myRequest.ContentLength = data.Length; 
    Stream newStream=myRequest.GetRequestStream(); 
    // Send the data. 
    newStream.Write(data,0,data.Length); 
    newStream.Close(); 
}