loginasync

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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();
}
}
}
}
 
}

  

posted @   ThisIsTest  阅读(92)  评论(0)    收藏  举报
编辑推荐:
· 如何统计不同电话号码的个数?—位图法
· C#高性能开发之类型系统:从 C# 7.0 到 C# 14 的类型系统演进全景
· 从零实现富文本编辑器#3-基于Delta的线性数据结构模型
· 记一次 .NET某旅行社酒店管理系统 卡死分析
· 长文讲解 MCP 和案例实战
阅读排行:
· C#高性能开发之类型系统:从 C# 7.0 到 C# 14 的类型系统演进全景
· 管理100个小程序-很难吗
· 基于Blazor实现的运输信息管理系统
· 如何统计不同电话号码的个数?—位图法
· 微信支付功能的设计实现与关键实践(UniApp+Java)全代码
点击右上角即可分享
微信分享提示