62,041
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 获取用户ID
/// </summary>
/// <param name="count">需要获取的用户数</param>
/// <param name="pageidx">当前页(默认为0)</param>
/// <param name="groupid">分组ID(默认为0,表示未分组的用户)</param>
/// <returns></returns>
public static ArrayList SubscribeMP(int? pagesize, int? pageidx, int? groupid)
{
try
{
/*获取用户信息的url,这里有几个参数给大家讲一下,1.token此参数为上面的token 2.pagesize此参数为每一页显示的记录条数3.pageid为当前的页数,4.groupid为微信公众平台的用户分组的组id*/
CookieContainer cookie = null;
string token = null;
cookie = WeiXinLoginCookie.LoginCookie;//取得cookie
token = WeiXinLoginCookie.Token;//取得token
string Url = " https://mp.weixin.qq.com/cgi-bin/contactmanage?t=user/index&pagesize=" + pagesize + "&pageidx=" + pageidx + "&type=0&token=" + token + "&lang=zh_CN";
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(Url);
webRequest2.CookieContainer = cookie;
webRequest2.ContentType = "text/html; charset=UTF-8";
webRequest2.Method = "GET";
webRequest2.ContentType = "application/x-www-form-urlencoded";
webRequest2.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
//webRequest2.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36";
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
string text2 = sr2.ReadToEnd();
UTF8Encoding utf8 = new UTF8Encoding();
Byte[] encodedBytes = utf8.GetBytes(text2);
String decodedString = utf8.GetString(encodedBytes);
text2 = decodedString;
MatchCollection mc;
//由于此方法获取过来的信息是一个html网页所以此处使用了正则表达式,注意:(此正则表达式只是获取了fakeid的信息如果想获得一些其他的信息修改此处的正则表达式就可以了。)
Regex r = new Regex("\"id\":\\d+,\"nick_name\""); //定义一个Regex对象实例
mc = r.Matches(text2);
Int32 friendSum = mc.Count; //好友总数
string fackID = "";
ArrayList fackID1 = new ArrayList();
for (int i = 0; i < friendSum; i++)
{
fackID = mc[i].Value.Split(new char[] { ':' })[1];
fackID = fackID.Replace(",\"nick_name\"", "").Trim();
fackID1.Add(fackID);
}
return fackID1;
}
catch (Exception ex)
{
throw new Exception(ex.StackTrace);
}
}
/// <summary>
/// 发送文字消息
/// </summary>
/// <param name="Message">消息内容</param>
/// <param name="fakeid">用户fakeid</param>
/// <returns></returns>
public static bool SendMsgText(string Message, string fakeid)
{
bool result = false;
string strMsg = System.Web.HttpUtility.UrlEncode(Message);
string padate = "type=1&content=" + strMsg + "&error=false&tofakeid=" + fakeid + "&token=" + WeiXinLoginCookie.Token + "&ajax=1";
string url = "https://mp.weixin.qq.com/cgi-bin/singlesend?t=ajax-response&lang=zh_CN";
byte[] byteArray = Encoding.UTF8.GetBytes(padate); // 转化
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(url);
webRequest2.CookieContainer = WeiXinLoginCookie.LoginCookie; //登录时得到的缓存
webRequest2.Referer = "https://mp.weixin.qq.com/cgi-bin/singlesendpage?t=message/send&action=index&tofakeid=" + fakeid + "&token=" + WeiXinLoginCookie.Token + "&lang=zh_CN";
webRequest2.Method = "POST";
webRequest2.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1";
webRequest2.ContentType = "application/x-www-form-urlencoded";
webRequest2.ContentLength = byteArray.Length;
Stream newStream = webRequest2.GetRequestStream();
// Send the data.
newStream.Write(byteArray, 0, byteArray.Length); //写入参数
newStream.Close();
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
StreamReader sr2 = new StreamReader(response2.GetResponseStream(), Encoding.Default);
string text2 = sr2.ReadToEnd();
if (text2.Contains("ok") || text2.Contains("10706") || text2.Contains("10703"))
{
result = true;
}
return result;
}