登录使用POST一个网站的Htt prequest [英] Login to a website using POST and HttpRequest
本文介绍了登录使用POST一个网站的Htt prequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个网站,我需要用的Htt prequest登录。说,网站的登录表单使用POST方法。我知道如何使用的Htt prequest没有保护的网页,我怎么可以登录到使用POST一个网站?
There is a website I need to login using HttpRequest. The login form of said website uses POST method. I know how to use HttpRequest for pages with no protection, how can I login to a website using POST?
推荐答案
这个例子是礼貌的http:// WWW .netomatix.com 。
该 POST
设置为 HttpWebRequest.Method
属性在的OnClick
处理程序背后code。
This example is by the courtesy of http://www.netomatix.com.
The POST
is set to the HttpWebRequest.Method
property in the OnClick
handler in code behind.
示例的形式:
<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="me@mybiz.com">
<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>
后面的 code:
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();
}
这篇关于登录使用POST一个网站的Htt prequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文