自动发贴
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
CSDN cs = new CSDN();
if (cs.Login("qq578023708", "*************")) cs.Reply("390521782","帮你顶了,一定要给分哦!");
Console.ReadLine();
}
}
class CSDN {
HttpWebRequest httpq = null;
HttpWebResponse httpp = null;
CookieCollection cc = null;
string cook = null;
/// <summary>
/// 登陆
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="pwd">密码</param>
/// <returns>返回TRUE 成功 返回 FALSE 失败</returns>
public bool Login(string userName, string pwd) {
httpq =(HttpWebRequest) WebRequest.Create("https://passport.csdn.net/ajax/accounthandler.ashx?t=log&u="+userName+"&p="+pwd+"&remember=0&f=http%3A%2F%2Fbbs.csdn.net%2Ftopics%2F390521782&rand=0.5834313757092409");
httpq.Accept = "*/*";
httpq.Referer = "https://passport.csdn.net/account/loginbox?callback=logined&hidethird=1&from=http%3a%2f%2fbbs.csdn.net%2ftopics%2f390521782";
httpq.CookieContainer = new CookieContainer();
httpp =(HttpWebResponse) httpq.GetResponse();
httpp.Cookies = cc;
StreamReader sr = new StreamReader(httpp.GetResponseStream());
cook = httpp.Headers["set-cookie"];
string re = sr.ReadToEnd();
if (re.IndexOf("\"status\":true") != -1) return true;
else return false;
}
/// <summary>
/// 回帖
/// </summary>
/// <param name="id">帖子ID</param>
/// <param name="text">回帖内容</param>
/// <returns></returns>
public string Reply(string id,string text) {
httpq = (HttpWebRequest)WebRequest.Create("http://bbs.csdn.net/topics/"+id+"?page=1");
httpq.Accept = "application/json, text/javascript, */*";
httpq.Referer = "http://bbs.csdn.net/topics/"+id+"?page=1";
StreamReader ssr = new StreamReader( httpq.GetResponse().GetResponseStream());
string RR = ssr.ReadToEnd();
int index1= RR.IndexOf("csrf-param");
int index2 = RR.IndexOf("content", index1);
index2 += 9;
index1 = RR.IndexOf(" ", index2)-1;
string token= RR.Substring(index2, index1 - index2);
httpq = (HttpWebRequest)WebRequest.Create("http://bbs.csdn.net/posts?topic_id="+id);
httpq.Referer = "http://bbs.csdn.net/topics/" + id + "?page=1";
httpq.Accept = "application/json, text/javascript, */*";
foreach (Cookie co in cc) {
httpq.CookieContainer.Add(co);
}
httpq.Method = "post";
httpq.ContentType = "application/x-www-form-urlencoded";
byte[] bytes = Encoding.UTF8.GetBytes("utf8=%E2%9C%93&authenticity_token="+HttpUtility.UrlEncode(token)+"&post%5Bbody%5D="+HttpUtility.UrlEncode(text)+"&commit=%E6%8F%90%E4%BA%A4%E5%9B%9E%E5%A4%8D");
httpq.ContentLength = bytes.Length;
httpq.GetRequestStream().Write(bytes, 0, bytes.Length);
httpp =(HttpWebResponse) httpq.GetResponse();
StreamReader sr = new StreamReader(httpp.GetResponseStream());
return sr.ReadToEnd();
}
}
}
评论列表