learn from your friends' implementation methods (the Public Account actively sends messages to users in Asp.net ).
///
// MD5 32-bit encryption
///
///
//
static string getmd5str32 (string Str)
{< br> 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 {< br> Buf [I] = (byte) temp [I];
}< br> 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 {< br> sbuilder. append (data [I]. tostring ("X2");
}< br> // return the hexadecimal string.
return sbuilder. tostring ();
}
Public static bool execlogin ()
{
Bool result = false;
String Password = getmd5str32 (strmppassword). toupper (); // convert to uppercase
String padata = "username =" + system. Web. httputility. urlencode (strmpaccount) + "& Pwd =" + password + "& imgcode = & F = JSON ";
String url = "http://mp.weixin.qq.com/cgi-bin/login? Lang = zh_cn "; // the login request URL
Try
{
Cookiecontainer cc = new cookiecontainer (); // receives the cache
Byte [] bytearray = encoding. utf8.getbytes (padata); // convert
Httpwebrequest webrequest2 = (httpwebrequest) webrequest. Create (URL );
Webrequest2.cookiecontainer = cc;
Webrequest2.method = "Post ";
Webrequest2.contenttype = "application/X-WWW-form-urlencoded ";
Webrequest2.contentlength = bytearray. length;
Stream newstream = webrequest2.getrequeststream ();
// Send the data.
Newstream. Write (bytearray, 0, bytearray. Length); // write Parameters
Newstream. Close ();
Httpwebresponse response2 = (httpwebresponse) webrequest2.getresponse ();
Streamreader Sr2 = new streamreader (response2.getresponsestream (), encoding. Default );
String text2 = sr2.readtoend ();
// newtonsoft is used for serialization.
sunnyinfo. web. class. weixinretinfo retinfo = newtonsoft. JSON. jsonconvert. deserializeobject (text2);
string token = string. empty;
If (retinfo. errmsg. length> 0)
{< br> token = retinfo. errmsg. split (New char [] {'&'}) [2]. split (New char [] {'='}) [1]. tostring (); // get the token
logininfo. logincookie = cc;
logininfo. createdate = datetime. now;
logininfo. token = token;
result = true;
}< BR >}< br> catch (exception ex)
{< br> company. pubclass. writelog ("erro [" + datetime. now. tostring () + "]" + ex. stacktrace);
}< br> return result;
}
Public static class logininfo
{
/// <Summary>
/// Token obtained after Logon
/// </Summary>
Public static string token {Get; set ;}
/// <Summary>
/// Cookie obtained after Logon
/// </Summary>
Public static cookiecontainer logincookie {Get; set ;}
/// <Summary>
/// Creation Time
/// </Summary>
Public static datetime createdate {Get; set ;}
}
// Send information
Public static bool sendmessage (string message, string fakeid)
{
Bool result = false;
Cookiecontainer cookie = NULL;
String token = NULL;
// The purpose here is to determine whether the cookie expires and obtain it again if it expires. The method for obtaining the cookie is self-contained in the active push information of the. NET public platform. You can take a look. No longer stick hereSource code.
If (null = Class. weixinlogin. logininfo. logincookie | class. weixinlogin. logininfo. createdate. addminutes (convert. toint32 (class. weixinlogin. strloingminutes) <datetime. Now)
{
Class. weixinlogin. execlogin ();
}
Cookie = Class. weixinlogin. logininfo. logincookie; // obtain the cookie
Token = Class. weixinlogin. logininfo. Token; // get token
String strmsg = system. Web. httputility. urlencode (Message );
String padate = "type = 1 & content =" + strmsg + "& error = false & tofakeid =" + fakeid + "& token =" + 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); // convert
Httpwebrequest webrequest2 = (httpwebrequest) webrequest. Create (URL );
Webrequest2.cookiecontainer = cookie; // cache obtained upon Logon
Webrequest2.referer = "https://mp.weixin.qq.com/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); // write Parameters
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;
}
TheCodeIt is already used in the project. You are welcome to discuss it.