This article introduces the development of sending messages. this content is the development of sending messages.
1, first, get the developer test account (apply), will generate the test account according to the account provided by the current scan code: link address: http://mp.weixin.qq.com/wiki/home/index.html
At this time, you can obtain the appid and appsecrept used for testing, and then call the GET interface to call the credential interface to obtain the access_token;
2. The following describes how to send messages, simulating single-user message sending and multi-user message batch sending.
(1) Basic methods and http methods
////// Http get/post public method //////Request link///Request parameter value (if it is a get method, the value is "" by default "")///Request method post or get///
Public static string Request (this string requestUrl, string requestMethod, string requestJsonParams = "") {string returnText = ""; StreamReader streamReader = null; HttpWebRequest request = null; HttpWebResponse response = null; encoding encoding = Encoding. UTF8; request = (HttpWebRequest) WebRequest. create (requestUrl); request. method = requestMethod; if (! String. isNullOrEmpty (requestJsonParams) & requestMethod. toLower () = "post") {byte [] buffer = encoding. getBytes (requestJsonParams); request. contentLength = buffer. length; request. getRequestStream (). write (buffer, 0, buffer. length);} try {response = (HttpWebResponse) request. getResponse (); using (streamReader = new StreamReader (response. getResponseStream (), System. text. encoding. getEncoding ("gb2312") // UTF-8 {returnText = streamReader. readToEnd () ;}} catch (Exception ex) {returnText = ex. message;} return returnText ;}
(2) sending simulation:
////// Send information (sent by a single user )//////Authorization code (token)///Sending information model///
Public static string SendSingleMessage (WeChatParamEntity messageInfo, string access_token) {messageInfo. MsgType = string. IsNullOrEmpty (messageInfo. MsgType )? "Text": messageInfo. MsgType; string jsonDataParams = messageInfo = null? "": JsonConvert. serializeObject (new {touser = messageInfo. toUser, msgtype = messageInfo. msgType, text = new {content = messageInfo. text}); string requestUrl = string. format (Consts. URL_POSTSINGLETEXTMESSAGE, access_token); return requestUrl. request ("POST", jsonDataParams );}////// Send information (multiple users batch send )//////Authorization code (token)///Sending information model///
Public static string SendMessages (WeChatParamsEntity messageInfo, string access_token) {messageInfo. MsgType = string. IsNullOrEmpty (messageInfo. MsgType )? "Text": messageInfo. MsgType; string jsonDataParams = messageInfo = null? "": JsonConvert. serializeObject (new {touser = messageInfo. toUser, msgtype = messageInfo. msgType, text = new {content = messageInfo. text}); string requestUrl = string. format (Consts. URL_POSTTEXTMESSAGES, access_token); return requestUrl. request ("POST", jsonDataParams );}
(3) two parameter models:
////// Entity model for sending information parameters ///Public class WeChatParamEntity {////// Openid of a common user ///Public string ToUser {get; set ;}////// Transfer file type (text, image, and so on )///Public string MsgType {get; set ;}= "text ";////// Transfer text content ///Public string Text {get; set ;}}////// Entity model for sending information parameters ///Public class WeChatParamsEntity {////// Openid of a common user ///Public string [] ToUser {get; set ;}////// Transfer file type (text, image, and so on )///Public string MsgType {get; set ;}= "text ";////// Transfer text content ///Public string Text {get; set ;}}
(4) links in web. config
3. the test uses the parameter touser. this is the openID of the object to be sent. this is very simple. it is obtained in the developer documentation (that is, in step 2 above ).
When appid and appsecrept are used, there is a QR code under the current page. several people can use a scan to automatically obtain the openID. at this time, the parameter is introduced to the script simulation.
Post.
Note: json parameter format prompted in this document
Note 3: The token validity period is 7200, which is two hours. you need to determine the token validity of the currently sent message user, and the maximum number of requests per day is 2000.
Get token:
# Region obtain the token and verify that the token expiration time is public static string GetAccessToken (string appid, string appSecret) {string token = ""; string requestUrl = string. format (ConfigBLL. URL_GETACCESSTOKEN, appid, appSecret); string requestResult = WebAPITransfer. request (requestUrl, "GET", ""); CommonBLL. debugLog (requestResult, "AccessToken-token-Params"); string [] strArray = requestResult. split (','); token = strArray [0]. split (':') [1]. replace ("\" "," "); return token;} # endregion
The above is the detailed content of message sending developed. For more information, see other related articles on php Chinese network!