没有合适的资源?快使用搜索试试~ 我知道了~
C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
3星 · 超过75%的资源 需积分: 46 119 下载量 130 浏览量
2017-11-11
14:26:39
上传
评论 2
收藏 50KB DOC 举报
温馨提示
试读
2页
C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
资源推荐
资源详情
资源评论
C#实现通过 HttpWebRequest 发送 POST 请求实现网站自动登陆
怎样通过 HttpWebRequest发送 POST请求到一个网页服务器?例如编写个程序实现自动
用户登录,自动提交表单数据到网站等。
假如某个页面有个如下的表单(Form):
<formname="form1"action="http://www.breakn.com/login.asp"method="post
">
<inputtype="text"name="userid"value="">
<inputtype="password"name="password"value="">
</form>
从表单可看到表单有两个表单域,一个是 userid 另一个是 password,所以以 POST 形式提
交的数据应该包含有这两项。
其中 POST 的数据格式为:
表单域名称 1=值 1&表单域名称 2=值 2&表单域名称 3=值 3……
要注意的是“值”必须是经过 HTMLEncode 的,即不能包含“<>=&”这些符号。
本例子要提交的数据应该是:
userid=value1&password=value2
用 C#写提交程序:
stringstrId="guest";
stringstrPassword="123456";
ASCIIEncodingencoding=newASCIIEncoding();
stringpostData="userid="+strId;
postData+=("&password="+strPassword);
byte[]data=encoding.GetBytes(postData);
//Preparewebrequest
HttpWebRequestmyRequest=
(HttpWebRequest)WebRequest.Create("http://www.here.com/login.asp");
myRequest.Method="POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength=data.Length;
StreamnewStream=myRequest.GetRequestStream();
//Sendthedata.
newStream.Write(data,0,data.Length);
newStream.Close();
//Getresponse
HttpWebResponsemyResponse=(HttpWebResponse)myRequest.GetResponse();
StreamReaderreader=newStreamReader(response.GetResponseStream(),Enc
oding.Default);
资源评论
- wufang_20042018-11-08很遗憾,没有用上
- oumiga1112018-05-29好用的资源
- 头戴大檐帽2018-11-28对我参考意义不大,谢谢
- helloworldV2019-09-16网上随便可以搜到的东西,浪费积分
xwzj264
- 粉丝: 22
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功