loginasync
public class CommonsAsync { public CookieContainer cookie = new CookieContainer(); private string loginUrl = "http://passport.cnblogs.com/login.aspx"; string loginData = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwULLTE1MzYzODg2NzZkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQtjaGtSZW1lbWJlcm1QYDyKKI9af4b67Mzq2xFaL9Bt&__EVENTVALIDATION=%2FwEdAAUyDI6H%2Fs9f%2BZALqNAA4PyUhI6Xi65hwcQ8%2FQoQCF8JIahXufbhIqPmwKf992GTkd0wq1PKp6%2B%2F1yNGng6H71Uxop4oRunf14dz2Zt2%2BQKDEIYpifFQj3yQiLk3eeHVQqcjiaAP&tbUserName=ListenCode&tbPassword=xiao123&btnLogin=%E7%99%BB++%E5%BD%95&txtReturnUrl=http%3A%2F%2Fhome.cnblogs.com%2Fing%2Fmy"; string commentData = "{\"ContentID\":209916,\"Content\":\"ceshi。\",\"strComment\":\"\",\"parentCommentId\":\"0\",\"title\":\"ddddd\"}"; public CommonsAsync() { } HttpWebRequest request = null; HttpWebRequest newRequest = null; public void Login() { request = (HttpWebRequest)HttpWebRequest.Create(loginUrl); request.Method = "Post"; request.ContentType = "application/x-www-form-urlencoded"; request.KeepAlive = true; request.CookieContainer = cookie; request.BeginGetRequestStream(new AsyncCallback(BeginRequest), new string[] { loginData, "login" }); } public void NewComments(CookieContainer cookie) { string url = "http://news.cnblogs.com/mvcajax/news/InsertComment"; newRequest = (HttpWebRequest)HttpWebRequest.Create(url); newRequest.Method = "Post"; newRequest.ContentType = "application/json; charset=UTF-8"; newRequest.KeepAlive = true; newRequest.CookieContainer = cookie; newRequest.ProtocolVersion = HttpVersion.Version11; newRequest.BeginGetRequestStream(new AsyncCallback(BeginRequest), new string[] { commentData, "news" }); } public void BeginRequest(IAsyncResult asy) { string[] re = asy.AsyncState as string[]; byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(re[0]); if (re[1].Equals("news")) { Stream sr = newRequest.GetRequestStream(); sr.Write(postBytes, 0, postBytes.Length); sr.Close(); newRequest.BeginGetResponse(new AsyncCallback(BeginResponse), re); } else if (re[1].Equals("login")) { Stream sr = request.GetRequestStream(); sr.Write(postBytes, 0, postBytes.Length); sr.Close(); request.BeginGetResponse(new AsyncCallback(BeginResponse), re); } } public void BeginResponse(IAsyncResult asy) { if (asy.AsyncState != null) { string[] re = asy.AsyncState as string[]; HttpWebResponse response = null; if (re[1].Equals("news")) { response = (HttpWebResponse)newRequest.EndGetResponse(asy); } else if (re[1].Equals("login")) { response = (HttpWebResponse)request.EndGetResponse(asy); } using (Stream sr = response.GetResponseStream()) { using (StreamReader sre = new StreamReader(sr, System.Text.Encoding.UTF8)) { string s = sre.ReadToEnd(); } } } } }