日期:2014-05-20 浏览次数:20871 次
private string accept = "*/*";
private string contentType = "application/x-www-form-urlencoded";
private string refer = "http://space.yuanchuang.com/account/login?ReturnUrl=http%3A//bbs.yuanchuang.com/forum.php";
private string userAgent = "Mozilla/5.0 (Windows NT 5.2) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1171.0 Safari/537.1";
private CookieContainer cookies = new CookieContainer();
private void button1_Click(object sender, EventArgs e)
{
string forWard = "http://bbs.yuanchuang.com/forum.php";
string userName = "123456"; //帐号
string passWord = "123456; //密码
string url = "http://space.yuanchuang.com/account/login";
string postData = string.Format("forward={0}&username={1}&password={2}", forWard, userName, passWord);
textBox1.Text = GetPost(url, "post", postData);//获取源代码
}
#region 获取数据方法
public string GetPost(string url, string method, string data)
{
Uri uri = new Uri(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.UserAgent = this.userAgent;
request.Accept = this.accept;
request.ContentType = this.contentType;
request.Method = method;
request.Referer = this.refer;
request.CookieContainer = this.cookies;
if (method.Equals("post"))
{
byte[] byteRequest = Encoding.Default.GetBytes(data);