会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
万德源
博客园
首页
博问
闪存
新随笔
订阅
管理
C# 模拟用户登录
///
<summary>
///
登录网站并获取Cookies
///
</summary>
///
<returns>
成功登录的Cookie信息
</returns>
public
static
CookieContainer Get_Login()
{
CookieContainer cc
=
new
CookieContainer();
string
FormURL
=
http://www.fy681.com/login.aspx
;
//
处理表单的绝对URL地址
string
FormData
=
"
username=dahai163&passwd=123
"
;
//
表单需要提交的参数,注意改为你已注册的信息。
ASCIIEncoding encoding
=
new
ASCIIEncoding();
byte
[] data
=
encoding.GetBytes(FormData);
HttpWebRequest request
=
(HttpWebRequest)WebRequest.Create(FormURL);
request.Method
=
"
POST
"
;
//
数据提交方式
request.ContentType
=
"
application/x-www-form-urlencoded
"
;
request.ContentLength
=
data.Length;
request.UserAgent
=
"
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)
"
;
//
模拟一个UserAgent
Stream newStream
=
request.GetRequestStream();
newStream.Write(data,
0
, data.Length);
newStream.Close();
request.CookieContainer
=
cc;
HttpWebResponse response
=
(HttpWebResponse)request.GetResponse();
cc.Add(response.Cookies);
Stream stream
=
response.GetResponseStream();
string
WebContent
=
new
StreamReader(stream, System.Text.Encoding.Default).ReadToEnd();
return
cc;
}
posted on
2014-02-21 11:51
万德源
阅读(
814
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部