using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
namespace VoteTool.Util
{
public class VoteUtil
{
/// <summary>
/// 执行投票
/// </summary>
/// <param name="param"></param>
/// <param name="referer"></param>
/// <param name="cookie"></param>
/// <returns></returns>
public static string ExecuteVote(string param, string referer, string cookie)
{
string url = "https://jingyan.baidu.com/submit/exp";
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
dic.Add("Accept-Encoding", "gzip, deflate, br");
dic.Add("X-Requested-With", "XMLHttpRequest");
dic.Add("Cookie", cookie);
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(url);
request.Host = "jingyan.baidu.com";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0";
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "*/*";
request.KeepAlive = true;
request.Referer = referer;
if (dic != null && dic.Count != 0)
{
foreach (var item in dic)
{
request.Headers.Add(item.Key, item.Value);
}
}
byte[] payload = System.Text.Encoding.UTF8.GetBytes(param);
request.ContentLength = payload.Length;
string strValue = "";
try
{
Stream writer = request.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
string StrDate = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate;
}
}
catch (Exception e)
{
strValue = e.Message;
}
return strValue;
}
/// <summary>
/// 获取当前日期
/// </summary>
/// <returns></returns>
public static DateTime GetNowTime()
{
try
{
//创建httpWebRequest对象
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("http://quan.suning.com/getSysTime.do");
//设置属性,初始化HttpWebRequest对象
Request.Method = "GET";
Request.Timeout = 30000;
Request.ContentType = "application/x-www-form-urlencoded";
// 读取服务器的返回信息
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
//将信息流转换为字符串
StreamReader Reader = new StreamReader(Response.GetResponseStream(), Encoding.UTF8);
var data = Reader.ReadToEnd();//以字符串形式返回信息流
Response.Close();//关闭响应释放资源
Reader.Close();//关闭流,释放资源
Regex reg = new Regex(@"([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))");
return Convert.ToDateTime(reg.Matches(data)[0].Value);
}
catch (Exception)
{
throw new Exception("网络异常,请稍后再试");
}
}
/// <summary>
/// 抓取悬赏经验
/// </summary>
/// <param name="url"></param>
/// <param name="isOver"></param>
/// <returns></returns>
public static List<string> GetXSInfo(string url, out bool isOver)
{
List<string> list = new List<string>();
//创建httpWebRequest对象
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(url);
//设置属性,初始化HttpWebRequest对象
Request.Method = "GET";
Request.Timeout = 30000;
Request.ContentType = "application/x-www-form-urlencoded";
// 读取服务器的返回信息
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
//将信息流转换为字符串
StreamReader Reader = new StreamReader(Response.GetResponseStream(), Encoding.UTF8);
var data = Reader.ReadToEnd();//以字符串形式返回信息流
Response.Close();//关闭响应释放资源
Reader.Close();//关闭流,释放资源
Regex reg = new Regex("<span class=\"cash\" style=\"width: 40px;\">¥[\\s\\S]{3,10}</span><a class=\"title query-item-id\" target=\"_blank\" data-queryId=\"[\\s\\S]{10,30}\">[\\s\\S]{1,40}</a>");
MatchCollection results = reg.Matches(data);
//是否抓取结束
isOver = data.Contains(">下一页><") == false;
foreach (Match item in results)
{
//没有下一页代表是尾页了
data = item.Value;
reg = new Regex("[A-Za-z0-9]{24}");
string id = reg.Matches(data)[0].Value;
string money = data.Replace("<span class=\"cash\" style=\"width: 40px;\">", "").Split('<')[0].Replace("¥", "");
var arrays = data.Replace("</a>", "").Split('>');
string name = arrays[arrays.Length - 1];
list.Add(id + "|" + money + "|" + name);
}
return list;
}
/// <summary>
/// 获取经验地址
/// </summary>
/// <param name="uid"></param>
/// <param name="no"></param>
/// <returns></returns>
public static List<string> GetExpInfo(string url)
{
List<string> list = new List<string>();
//创建httpWebRequest对象
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(url);
//设置属性,初始化HttpWebRequest对象
Request.Method = "GET";
Request.Timeout = 30000;
Request.ContentType = "application/x-www-form-urlencoded";
// 读取服务器的返回信息
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
//将信息流转换为字符串
StreamReader Reader = new StreamReader(Response.GetResponseStream(), Encoding.UTF8);
var data = Reader.ReadToEnd();//以字符串形式返回信息流
Response.Close();//关闭响应释放资源
Reader.Close();//关闭流,释放资源
Regex reg = new Regex("<p class=\"tit\">[\\s\\S]{1,5}(<span class=\"i-patch\" title=\"悬赏经验\"></span>[\\s\\S]{1,5}){0,1}<a href[\\s\\S]{30,120}>[\\s\\S]{0,120}</a>[\\s\\S]</p>");
MatchCollection results = reg.Matches(data);
foreach (Match item in results)
{
data = item.Value;
reg = new Regex("<a href[\\s\\S]{30,200}>[\\s\\S]{0,100}</a>");
data = reg.Matches(data)[0].Value;
data = data.Replace("<a", "").Replace("href", "").Replace("\"", "").Replace("=", "").Replace("title", "|").Replace("target", "|");
var arrys = data.Split('|');
list.Add("https://jingyan.baidu.com" + arrys[0].Trim() + "|" + arrys[1].Trim());
}
return list;
}
/// <summary>
/// 随机打乱数组
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static List<T> RandomSort<T>(List<T> list)
{
var random = new Random();
var newList = new List<T>();
foreach (var item in list)
{
newList.Insert(random.Next(newList.Count), item);
}
return newList;
}
/// <summary>
/// 收藏
/// </summary>
/// <param name="expId"></param>
/// <param name="referer"></param>
/// <param name="cookie"></param>
/// <returns></returns>
public static string FavorExp(string expId, string referer, string cookie)
{
string url = "https://jingyan.baidu.com/submit/favor";
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
dic.Add("Accept-Encoding", "gzip, deflate, br");
dic.Add("X-Requested-With", "XMLHttpRequest");
dic.Add("Cookie", cookie);
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(url);
request.Host = "jingyan.baidu.com";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0";
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "*/*";
request.KeepAlive = true;
request.Referer = referer;
if (dic != null && dic.Count != 0)
{
foreach (var item in dic)
{
request.Headers.Add(item.Key, item.Value);
}
}
string param = "method=addFavorExp&eidEnc=" + expId;
byte[] payload = System.Text.Encoding.UTF8.GetBytes(param);
request.ContentLength = payload.Length;
string strValue = "";
try
{
Stream writer = request.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
string StrDate = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate;
}
}
catch (Exception e)
{
strValue = e.Message;
}
return strValue;
}
/// <summary>
/// 领取悬赏
/// </summary>
/// <param name="param"></param>
/// <param name="referer"></param>
/// <param name="cookie"></param>
/// <returns></returns>
public static string ExecuteGetXS(string param, string referer, string cookie)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
string url = "https://jingyan.baidu.com/patchapi/claimQuery";
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(url);
request.Host = "jingyan.baidu.com";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0";
request.Accept = "*/*";
dic.Add("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
dic.Add("Accept-Encoding", "gzip, deflate, br");
request.Referer = referer;
//request.Referer = "https://jingyan.baidu.com/user/nucpage/patch?tab=highquality";
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
dic.Add("X-Requested-With", "XMLHttpRequest");
byte[] payload = System.Text.Encoding.UTF8.GetBytes(param);
request.ContentLength = payload.Length;
dic.Add("Origin", "https://jingyan.baidu.com");
request.KeepAlive = true;
dic.Add("Cookie", cookie);
dic.Add("Pragma", "no-cache");
dic.Add("Cache-Control", "no-cache");
request.Method = "POST";
if (dic != null && dic.Count != 0)
{
foreach (var item in dic)
{
request.Headers.Add(item.Key, item.Value);
}
}
string strValue = "";
try
{
Stream writer = request.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
string StrDate = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate;
}
}
catch (Exception e)
{
strValue = e.Message;
}
return strValue;
}
}
}
C# 模拟百度经验投票、收藏请求
最新推荐文章于 2024-11-06 09:41:08 发布