C# 使用POST和HttpRequest登录到网站

C# 使用POST和HttpRequest登录到网站,c#,.net,httprequest,C#,.net,Httprequest,有一个网站,我需要登录使用HttpRequest。该网站的登录形式采用POST方式。我知道如何对没有保护的页面使用HttpRequest,如何使用POST登录网站?此示例由提供。 POST设置为代码隐藏中的OnClick处理程序中的HttpWebRequest.Method属性 示例表单: <form name="_xclick" target="paypal" action="https://www.paypal.com/cgi-

有一个网站,我需要登录使用HttpRequest。该网站的登录形式采用POST方式。我知道如何对没有保护的页面使用HttpRequest,如何使用POST登录网站?

此示例由提供。
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="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>
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();
}

代码隐藏:

<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>
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();
}
private void on postinfoclick(对象发送者,System.EventArgs e)
{
string strId=UserId\u TextBox.Text;
字符串strName=Name\u TextBox.Text;
ascienceoding encoding=新的ascienceoding();
string postData=“userid=”+strId;
postData+=(“&username=“+strName”);
byte[]data=encoding.GetBytes(postData);
//准备web请求。。。
HttpWebRequestMyRequest=
(HttpWebRequest)WebRequest.Create(“http://localhost/MyIdentity/Default.aspx");

myRequest.Method=“POST”//向我们展示您已经尝试过的内容。