您的位置:首页 > 运维架构 > 网站架构

利用Cookie自动登录别人的网站

2010-02-26 16:59 429 查看
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;

public partial class Default3 : System.Web.UI.Page
{
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]

public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CookieContainer cc = new CookieContainer();
string postData = "do=login&username=xiaoliu007&passwd=xiaoliu";
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化

HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri("http://info.sporttery.cn/lotterygame/index.php"));
webRequest2.CookieContainer = cc;
webRequest2.Method = "POST";
webRequest2.ContentType = "application/x-www-form-urlencoded";
webRequest2.ContentLength = byteArray.Length;
Stream newStream = webRequest2.GetRequestStream();
// Send the data.
newStream.Write(byteArray, 0, byteArray.Length); //写入参数
newStream.Close();

HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
cc.Add(response2.Cookies);
foreach (Cookie cookie in response2.Cookies) //将cookie设置为浏览的cookie
{

InternetSetCookie(

"http://" + cookie.Domain.ToString(),

cookie.Name.ToString(),

cookie.Value.ToString() + ";expires=Sun,22-Feb-2099 00:00:00 GMT");

}

System.Diagnostics.Process.Start("http://info.sporttery.cn/lotterygame/combination_num.php?s='234'&values=',001001,001002,001003,002001,002002,002003,003001,004001'&sports='fb'"); //打开浏览器

StreamReader sr2 = new StreamReader(response2.GetResponseStream(),Encoding.GetEncoding("gb2312"));
string text2 = sr2.ReadToEnd();
Response.Write(text2);
sr2.Close();
response2.Close();

//Console.WriteLine(outdata);
//再次显示"登录"
//如果把*行注释调,就显示"没有登录"
}

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