【移动应用开发技术】C#实现微信公众号群发消息的案例_第1页
【移动应用开发技术】C#实现微信公众号群发消息的案例_第2页
【移动应用开发技术】C#实现微信公众号群发消息的案例_第3页
【移动应用开发技术】C#实现微信公众号群发消息的案例_第4页
【移动应用开发技术】C#实现微信公众号群发消息的案例_第5页
免费预览已结束,剩余2页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】C#实现微信公众号群发消息的案例

这篇文章将为大家详细讲解有关C#实现微信公众号群发消息的案例,在下觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。总体思路:1.首先必须要在微信公众平台上申请一个公众号。2.然后进行模拟登陆。(由于我对http传输原理和编程不是特别懂,在模拟登陆的地方,不是特别清楚,希望有大神指教)3.模拟登陆后会获得一个token(令牌)和cookie。4.因为模拟登陆后相当于就进入了微信公众平台,在这个里面就可以抓取到需要的数据,如公众好友的昵称,fakeId。其中的fakeid非常重要,因为传输数据必须要知道对方的fakeid。5.知道对方的fakeid就可以进行数据的发送了。不过里面还有一些小问题,希望有人继续修改和讨论!也有人说这样会被封号,所以请谨慎操作讲一下我项目里面的主要内容1.WeiXinLogin.cs类是用来执行登陆功能的//对密码进行MD5加密

static

string

GetMd5Str32(string

str)

{

MD5CryptoServiceProvider

md5Hasher

=

new

MD5CryptoServiceProvider();

//

Convert

the

input

string

to

a

byte

array

and

compute

the

hash.

char[]

temp

=

str.ToCharArray();

byte[]

buf

=

new

byte[temp.Length];

for

(int

i

=

0;

i

<

temp.Length;

i++)

{

buf[i]

=

(byte)temp[i];

}

byte[]

data

=

md5Hasher.ComputeHash(buf);

//

Create

a

new

Stringbuilder

to

collect

the

bytes

//

and

create

a

string.

StringBuilder

sBuilder

=

new

StringBuilder();

//

Loop

through

each

byte

of

the

hashed

data

//

and

format

each

one

as

a

hexadecimal

string.

for

(int

i

=

0;

i

<

data.Length;

i++)

{

sBuilder.Append(data[i].ToString("x2"));

}

//

Return

the

hexadecimal

string.

return

sBuilder.ToString();

}

//执行登陆操作

public

static

bool

ExecLogin(string

name,string

pass)

{

bool

result

=

false;

string

password

=

GetMd5Str32(pass).ToUpper();

string

padata

=

"username="

+

name

+

"&pwd="

+

password

+

"&imgcode=&f=json";

string

url

=

"/cgi-bin/login?lang=zh_CN

";//请求登录的URL

try

{

CookieContainer

cc

=

new

CookieContainer();//接收缓存

byte[]

byteArray

=

Encoding.UTF8.GetBytes(padata);

//

转化

HttpWebRequest

webRequest2

=

(HttpWebRequest)WebRequest.Create(url);

//新建一个WebRequest对象用来请求或者响应url

webRequest2.CookieContainer

=

cc;

//保存cookie

webRequest2.Method

=

"POST";

//请求方式是POST

webRequest2.ContentType

=

"application/x-www-form-urlencoded";

//请求的内容格式为application/x-www-form-urlencoded

webRequest2.ContentLength

=

byteArray.Length;

Stream

newStream

=

webRequest2.GetRequestStream();

//返回用于将数据写入

Internet

资源的

Stream。

//

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();

//此处用到了newtonsoft来序列化

WeiXinRetInfo

retinfo

=

Newtonsoft.Json.JsonConvert.DeserializeObject<WeiXinRetInfo>(text2);

string

token

=

string.Empty;

if

(retinfo.ErrMsg.Length

>

0)

{

token

=

retinfo.ErrMsg.Split(new

char[]

{

'&'

})[2].Split(new

char[]

{

'='

})[1].ToString();//取得令牌

LoginInfo.LoginCookie

=

cc;

LoginInfo.CreateDate

=

DateTime.Now;

LoginInfo.Token

=

token;

result

=

true;

}

}

catch

(Exception

ex)

{

throw

new

Exception(ex.StackTrace);

}

return

result;

}

public

static

class

LoginInfo

{

///

<summary>

///

登录后得到的令牌

///

</summary>

public

static

string

Token

{

get;

set;

}

///

<summary>

///

登录后得到的cookie

///

</summary>

public

static

CookieContainer

LoginCookie

{

get;

set;

}

///

<summary>

///

创建时间

///

</summary>

public

static

DateTime

CreateDate

{

get;

set;

}

}2.在WeiXin.cs类中实现发送数据public

static

bool

SendMessage(string

Message,

string

fakeid)

{

bool

result

=

false;

CookieContainer

cookie

=

null;

string

token

=

null;

cookie

=

WeiXinLogin.LoginInfo.LoginCookie;//取得cookie

token

=

WeiXinLogin.LoginInfo.Token;//取得token

string

strMsg

=

System.Web.HttpUtility.UrlEncode(Message);

//对传递过来的信息进行url编码

string

padate

=

"type=1&content="

+

strMsg

+

"&error=false&tofakeid="

+

fakeid

+

"&token="

+

token

+

"&ajax=1";

string

url

=

"/cgi-bin/singlesend?t=ajax-response&lang=zh_CN";

byte[]

byteArray

=

Encoding.UTF8.GetBytes(padate);

//

转化

HttpWebRequest

webRequest2

=

(HttpWebRequest)WebRequest.Create(url);

webRequest2.CookieContainer

=

cookie;

//登录时得到的缓存

webRequest2.Referer

=

"/cgi-bin/singlemsgpage?token="

+

token

+

"&fromfakeid="

+

fakeid

+

"&msgid=&source=&count=20&t=wxm-singlechat&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"))

{

result

=

true;

}

return

result;

}3.SendMessage.aspx.cs中主要实现获取fakeidpublic

static

ArrayList

SubscribeMP()

{

try

{

CookieContainer

cookie

=

null;

string

token

=

null;

cookie

=

WeiXinLogin.LoginInfo.LoginCookie;//取得cookie

token

=

WeiXinLogin.LoginInfo.Token;//取得token

/*获取用户信息的url,这里有几个参数给大家讲一下,1.token此参数为上面的token

2.pagesize此参数为每一页显示的记录条数

3.pageid为当前的页数,4.groupid为微信公众平台的用户分组的组id,当然这也是我的猜想不一定正确*/

string

Url

=

"/cgi-bin/contactmanagepage?t=wxm-friend&token="

+

token

+

"&lang=zh_CN&pagesize=10&pageidx=0&type=0&groupid=0";

HttpWebRequest

webRequest2

=

(HttpWebRequest)WebRequest.Create(Url);

webRequest2.CookieContainer

=

cookie;

webRequest2.ContentType

=

"text/html;

charset=UTF-8";

webRequest2.Method

=

"GET";

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";

HttpWebResponse

response2

=

(HttpWebResponse)webRequest2.GetResponse();

StreamReader

sr2

=

new

StreamReader(response2.GetResponseStream(),

Encoding.Default);

string

text2

=

sr2.ReadToEnd();

MatchCollection

mc;

//由于此方法获取过来的信息是一个html网页所以此处使用了正则表

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论