SEO(Business)
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace goggb
{
class Business
{
public static CookieCollection cookies = new CookieCollection();
public static CookieContainer cookie = new CookieContainer();
private static string cookiesstr = "";
public static String token = "";
//admin888 adminadmin
//地址:
static string site = "http://admin.kuaipaitong.com";//http://kpt.ezhuoer.com
static string loginURL = site+"/go/index.php/Admin/Login/login.html";
//验证码获取地址:
static string verifyURL = site + "/go/index.php/Admin/Login/verify/";
//参数:admin_name用户名 admin_password密码verify验证码
//2、 获取任务 Get访问
static string getTaskURL = site + "/go/index.php/Admin/TaskHit/index?";//随机数
//3、 反馈结果
static string putTaskResultURL = site + "/go/index.php/Admin/TaskHit/clientinfo/?r=";
public static string ChangeImageToString(Image image)
{
try
{
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
string pic = Convert.ToBase64String(arr);
return pic;
}
catch (Exception)
{
return "Fail to change bitmap to string!";
}
}
public static Image ChangeStringToImage(string pic)
{
try
{
byte[] imageBytes = Convert.FromBase64String(pic);
//读入MemoryStream对象
MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
memoryStream.Write(imageBytes, 0, imageBytes.Length);
//转成图片
Image image = Image.FromStream(memoryStream);
return image;
}
catch (Exception)
{
Image image = null;
return image;
}
}
public static Image getVerify()
{
try {
string tempURL = verifyURL + getRandom();
LogHelper.Info(typeof(Business), "访问图片URL" + tempURL);
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(tempURL);
myHttpWebRequest.Timeout = 20 * 1000; //连接超时
myHttpWebRequest.Accept = "*/*";
myHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;)";
myHttpWebRequest.CookieContainer = new CookieContainer(); //暂存到新实例
HttpWebResponse webResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();//获取响应流
Bitmap sourcebm = new Bitmap(stream);
webResponse.Close();
cookiesstr = myHttpWebRequest.CookieContainer.GetCookieHeader(myHttpWebRequest.RequestUri); //把cookies转换成字符串
cookie = myHttpWebRequest.CookieContainer; //保存cookies
return sourcebm;
}catch(Exception ex){
LogHelper.Error(typeof(Business), "获取登录验证码出错。", ex);
}
return null;
}
public static string getRandom()
{
Random rd = new Random();
int temp = rd.Next();
string paramString = temp.ToString();
return paramString;
}
public static string Login(string userName, string password, string verify)
{
string paramString = "?admin_name=" + userName + "&admin_password=" + password + "&verify=" + verify;
return DoService(loginURL, paramString, Tools.getUUID());
}
/*
* Useragent必须包含:jqm=机器码(注:机器码取CPU序列号+mac地址后MD5,小写)
* 成功:{
"id": "254",任务id
"type": "1",任务类型1:排名点击2:搜索相关3:指数下拉4:广告点击
"searchsite": "1",搜索引擎1、百度2:搜狗3:好搜4:淘宝
"keyword": "淄博丽人医院",关键词
"site": "淄博的丽人医院怎么样",任务特征,标题或url包含该信息即点击
"page": "3",最大翻页数
"sitehit": "0",深入点击次数
"
* ": "30",等待时间(进入任务网站之后随机停留时间,随机数范围 取整(等待时间/2) 到 等待时间)
"abscissa": "0",横坐标(广告点击时用到,距离任务特征的距离)
"ordinate": "0",纵坐标
"subtask": "2",智能优化(若该条数不为0,则该任务含有子任务,除了点击包含site标志外,还需点击subtasksite0,subtasksite1…)
"subtaskinfo": {
"subtasksite0": "baidu.com",
"subtaskhistoryid0": 0,子任务回馈id
"subtasksite1": "ip138.com",
"subtaskhistoryid1": 0
},
"subtask": "1",智能优化
" subtaskhistoryid ": "0",子任务回馈id
"subtaskinfo": {
"site": "baidu.com"子任务
},
"taskhistoryid": 133619,任务回馈id
"task": "ok",
"alert": "任务分配成功!"
}
失败:{
"task": "fail",
"alert": "没有可分配任务!"
}
*/
public static string getTask()
{
string tempURL = getTaskURL + getRandom();
return DoService(tempURL, "", Tools.getUUID());
}
private static string DoService(string url, string paramString, String userAgent)
{
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
StreamReader reader = null;
try
{
LogHelper.Info(typeof(Business), "调用UEL:" + url + paramString + "&token=" + Main.tokens);
webRequest = (HttpWebRequest)HttpWebRequest.Create(url + paramString +"&token="+Main.tokens);
webRequest.Method = "POST";
webRequest.CookieContainer = cookie;
webRequest.Headers.Add("Cookie", cookiesstr);//包含与请求或响应关联的协议标头。
if (!String.IsNullOrEmpty(userAgent))
{
webRequest.UserAgent = "jqm="+userAgent;
}
webRequest.Timeout =20000;
webResponse = (HttpWebResponse)webRequest.GetResponse();
reader = new StreamReader(webResponse.GetResponseStream(),System.Text.Encoding.UTF8);//Encoding.GetEncoding("utf-8"));
string response = reader.ReadToEnd();//获取响应,读取从头读到尾
try
{
/*在服务器端,由Json字符串转换为.net对象优先使用JsonConvert对象的DeserializeObject方法*/
JObject task = (JObject)JsonConvert.DeserializeObject(response);
string responseZw =(string)task["alert"];
LogHelper.Info(typeof(Business), "调用http返回值转换成中文。" + responseZw);
}
catch(Exception ex){
LogHelper.Error(typeof(Business), "调用http返回值转换成中文出错。" + response, ex);
}
LogHelper.Info(typeof(Business), "调用http返回值。" + response);
return response;
}
catch (Exception ex)
{
LogHelper.Error(typeof(Business), "调用远程方法出错。" + url + paramString, ex);
return "";
}
finally
{
if (webResponse != null)
{
webResponse.Close();
cookie = webRequest.CookieContainer; //保存cookies
}
if (reader != null)
{
reader.Close();
}
}
}
private static Bitmap getImage(string url)
{
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
StreamReader reader = null;
try
{
webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
webRequest.Method = "GET";
webRequest.CookieContainer = new CookieContainer();
webRequest.CookieContainer.Add(cookies);
webRequest.Timeout = 20000;
webResponse = (HttpWebResponse)webRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
Bitmap sourcebm = new Bitmap(stream);
cookies = webResponse.Cookies;
return sourcebm;
}
catch (WebException ex)
{
return null;
}
finally
{
if (webResponse != null)
{
webResponse.Close();
}
if (reader != null)
{
reader.Close();
}
}
}
/// <summary>
/// 将Unicode编码转换为汉字字符串
/// </summary>
/// <param name="str">Unicode编码字符串</param>
/// <returns>汉字字符串</returns>
public static string ToGB2312(string str)
{
string r = "";
MatchCollection mc = Regex.Matches(str, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
byte[] bts = new byte[2];
foreach(Match m in mc )
{
bts[0] = (byte)int.Parse(m.Groups[2].Value, NumberStyles.HexNumber);
bts[1] = (byte)int.Parse(m.Groups[1].Value, NumberStyles.HexNumber);
r += Encoding.Unicode.GetString(bts);
}
return r;
}
/*
3、 反馈结果
Get请求http://kpt.ezhuoer.com/go/index.php/Admin/TaskHit/clientinfo/?r=4c0ad1a11616c7bcb31144d57838d75bb8197351ac14b77097e43862561f035007fd
Useragent必须包含:jqm=机器码(注:机器码取CPU序列号+mac地址后MD5,小写)
系统依据该特征判定是否为客户端请求,客户端请求返回json,非客户端返回html
必须带登陆成功后返回的Cookie
R参数为RC4加密,加密内容为:
id=133619&point=5&result=41&ip=127.0.0.1&error=fail
若所接受的任务包含子任务,则回馈内容应该加上以下部分:
Subid0=0&result0=41&subid1=0&result1=41 … (result0、result1为子任务排名,没有找到则为0)
Id为请求任务的回馈id
Subid子任务回馈id
Point为任务积分
Result为任务排名,0为失败,失败时需增加error参数注明失败原因
Ip为执行任务时浏览器所用ip,可以为空
成功:{
"task": "ok",
"alert": "完成任务【254】得到积分5分"
}
失败:{
"task": "fail",
"alert": "没有可分配任务!"
}
*/
//internal static string getTaskRsult(bool p1, string p2)
//{
// string url = site + "/go/index.php/Admin/TaskHit/clientinfo/?r=";
// string paramString="id="+p2+"&point=5&result=41&ip=127.0.0.1&error="+p1;
// String userAgent = "";
// DoService( url, paramString, userAgent);
// return "";
//}
public static string putTaskResult(string id, string point, string result,string error)
{
string r = "";
if (!error.Equals("") || result =="0")
{
error = "没有可分配任务";
r = @"id=" + id + "&point=" + point + "&result=" + result + "&ip=&error=" + error;
}
else
{
r = @"id=" + id + "&point=" + point + "&result=" + result + "&ip=&error=" + "任务完成!";
}
LogHelper.Info(typeof(Business), "提交任務前字符:" + r);
byte[] rByte = Encoding.UTF8.GetBytes(r);
RC4Crypto rc4 = new RC4Crypto();
byte[] byteArray = rc4.EncryptEx(rByte, "zdthnjikl");
string rTemp = ToHexString(byteArray);
LogHelper.Info(typeof(Business), "提交任务加密字符:" + rTemp);
string userAgent = Tools.getUUID();
return DoService(putTaskResultURL, rTemp, userAgent);
}
public static string StrToHex(string str)
{
string strResult;
byte[] buffer = Encoding.GetEncoding("utf-8").GetBytes(str);
strResult = "";
foreach (byte b in buffer)
{
strResult += b.ToString("X2");//X是16进制大写格式
}
return strResult;
}
public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
{
string hexString = string.Empty;
if (bytes != null)
{
StringBuilder strB = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
strB.Append(bytes[i].ToString("X2"));
}
hexString = strB.ToString();
} return hexString;
}
}
}