您的位置:首页 > 编程语言 > C#

C# 模拟用户登录

2014-02-21 11:51 239 查看
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;


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: