CookieContainer cc = new CookieContainer();
string FormURL = "http://passport.baidu.com/?login"; //处理表单的绝对URL地址
string FormData = "username=woowater" + "&password=MyPass"; //表单需要提交的参数,注意改为你已注册的信息。
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.KeepAlive = true;
request.Referer = "http://passport.baidu.com/";
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();
string PhotoClassURL = "http://hi.baidu.com/woowater/creat/blog";
HttpWebRequest Myrequest = (HttpWebRequest)WebRequest.Create(PhotoClassURL);
Myrequest.CookieContainer = cc;
HttpWebResponse Myresponse = (HttpWebResponse)Myrequest.GetResponse();
cc.Add(Myresponse.Cookies);
Stream Mystream = Myresponse.GetResponseStream();
string sHtml = new StreamReader(Mystream, System.Text.Encoding.Default).ReadToEnd();
Mystream.Close();
//sHtml即为你登录之后看到的内容.
// MessageBox.Show(sHtml);
string Poststr = string.Format("spBlogTitle=yidaxu001&spBlogText=yidaxu001001&spBlogCatName=默认分类&spBlogPower=0&spIsCmtAllow=1&ct=1&cm=1");
Poststr = EncodePost(Poststr);
byte[] PostData = encoding.GetBytes(Poststr);
HttpWebRequest PostRequest = (HttpWebRequest)WebRequest.Create("http://hi.baidu.com/woowater/commit");
PostRequest.Method = "POST"; //数据提交方式
PostRequest.ContentType = "application/x-www-form-urlencoded";
PostRequest.KeepAlive = true;
PostRequest.Referer = "http://hi.baidu.com/woowater/creat/blog";
PostRequest.ContentLength = PostData.Length;
PostRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
//模拟一个UserAgent
Stream PostnewStream = PostRequest.GetRequestStream();
PostnewStream.Write(PostData, 0, PostData.Length);
PostnewStream.Close();
PostRequest.CookieContainer = cc;
HttpWebResponse Postresponse = (HttpWebResponse)PostRequest.GetResponse();
cc.Add(Postresponse.Cookies);
Stream PostStream = Postresponse.GetResponseStream();
string PostWebContent = new StreamReader(PostStream, System.Text.Encoding.Default).ReadToEnd();
PostStream.Close();
MessageBox.Show(WebContent);
把异常处理代码去掉了
似乎还是某个地方模拟的不是很到位,使用httpwatch等工具再仔细看看!关注中...