zoukankan      html  css  js  c++  java
  • C#实现通过HttpWebRequest发送POST请求实现网站自动登陆

    
    

    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆

    
    
    怎样通过HttpWebRequest 发送 POST 请求到一个网页服务器?例如编写个程序实现自动用户登录,自动提交表单数据到网站等。
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆假如某个页面有个如下的表单(Form): 
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆<form name="form1" action="http://www.sina.com/login.asp" method="post">
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆<input type="text" name="userid" value="">
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆<input type="password" name="password" value="">
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆</form> 
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆从表单可看到表单有两个表单域,一个是userid另一个是password,所以以POST形式提交的数据应该包含有这两项。
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆其中POST的数据格式为:
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆表单域名称1=值1&表单域名称2=值2&表单域名称3=值3……
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆要注意的是“值”必须是经过HTMLEncode的,即不能包含“<>=&”这些符号。
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆本例子要提交的数据应该是:
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆userid=value1&password=value2
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆用C#写提交程序:
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆string strId = "guest";
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆string strPassword= "123456";
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆ASCIIEncoding encoding=new ASCIIEncoding();
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆string postData="userid="+strId;
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆postData += ("&password="+strPassword);
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆byte[] data = encoding.GetBytes(postData);
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆// Prepare web requestC#实现通过HttpWebRequest发送POST请求实现网站自动登陆
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆HttpWebRequest myRequest =
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆(HttpWebRequest)WebRequest.Create("http://www.sina.com/login.asp");
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆myRequest.Method = "POST";
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆myRequest.ContentType="application/x-www-form-urlencoded";
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆myRequest.ContentLength = data.Length;
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆Stream newStream=myRequest.GetRequestStream();
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆// Send the data.
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆newStream.Write(data,0,data.Length);
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆newStream.Close();
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆// Get response
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆HttpWebResponse myResponse=(HttpWebResponse)myRequest.GetResponse();
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.Default);
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆string content = reader.ReadToEnd();
    C#实现通过HttpWebRequest发送POST请求实现网站自动登陆Response.Write(content); 

    此方法核心问题是要找到请求页和表单域中的参数ID。

     
  • 相关阅读:
    nodejs安装
    mongodb安装指南
    JQuery控制input的readonly和disabled属性
    C# 汉子增加UTF-8头
    Microsoft Visual C++ 2005 SP1 Redistributable 安装错误
    sql server 查找指定字符串的位置
    sql server 数据库附加时程序集错误
    前端路由
    CSS学习笔记——选择器优先级
    CSS学习笔记——盒子模型
  • 原文地址:https://www.cnblogs.com/hbtmwangjin/p/11204586.html
Copyright © 2011-2022 走看看