HttpWEBRequest模拟登陆人人网
本人初学,照着网上的代码,改了传入参数,为何登陆不进去啊
using System;抓取采集 HTTPWebRequest
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Collections;
using System.Text;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
public static string PostAndGetHTML(string targetURL, CookieContainer cc, Hashtable param)
{
//prepare the submit data
string formData = "";
foreach (DictionaryEntry de in param)
{
formData += de.Key.ToString() + "=" + de.Value.ToString() + "&";
}
if (formData.Length > 0)
formData = formData.Substring(0, formData.Length - 1); //remove last '&'
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(formData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(targetURL);
request.Method = "POST"; //post
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1; .NET CLR 2.0.1124)";
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 result = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
return result;
}
protected void Button1_Click(object sender, EventArgs e)
{
CookieContainer cc = new CookieContainer();//this is for keep the Session and Cookie
Hashtable param = new Hashtable();//this is for keep post data.
string urlLogin = "http://www.renren.com/";
//下面的参数都是火狐firebug抓的都是真实的。
param.Add("captcha_type", "web_login");
param.Add("domain", "renren.com");
param.Add("email", "这里是我用户名");
param.Add("icode", "");
param.Add("key_id", "1");
param.Add("origURL", "http://www.renren.com/indexcon");
param.Add("password", "d0cf42c2d3d337f9e5d14083f2d52cb2");
param.Add("rkey", "d0cf42c2d3d337f9e5d14083f2d52cb2");
string result = PostAndGetHTML(urlLogin, cc, param);
Response.Write(result);//最后显示的竟然还是登陆页面,为什么呢
}
}