Some time ago to push back, Android used Baidu push service, because it is just a simple use of the Android push notification function, so it is not recommended by Baidu C # SDK, through the use of various information on the Internet and Baidu API, the eldest brother finally toss out a piece of code that can be used (early know so tangled, Directly with someone else's ... Obsessive-compulsive disorder doesn't hurt.
/// <summary> ///Baidu Push Send/// </summary> Public classBaidupush {Private Const stringMETHOD ="push_msg"; Private Const stringURL ="Https://channel.api.duapp.com/rest/2.0/channel/channel"; Private Const stringApikey ="xxxxxxxxxxxxxxxxx"; Private Const stringSecret_key ="xxxxxxxxxxxxxxxxxxxxxxxxxxxx"; Public Static stringPush (stringUSER_ID,stringTitlestringDescriptionstringMsg_keys) {DateTime UtcNow=Datetime.utcnow; DateTime Epoch=NewDateTime (1970,1,1,0,0,0); UINTTimestamp = (UINT) (UtcNow-epoch). TotalSeconds; UINTexpires = (UINT) (Utcnow.adddays (1)-Epoch. TotalSeconds;//expires after one dayDictionary<string,string> dic =Newdictionary<string,string>(); Dic. ADD ("Method", method); Dic. ADD ("Apikey", Apikey); Dic. ADD ("timestamp", timestamp. ToString ()); Dic. ADD ("Expires", expires. ToString ()); Dic. ADD ("Push_type","1");// UnicastDic. ADD ("Device_type","3");//andriod EquipmentDic. ADD ("user_id", user_id); Dic. ADD ("Message_type","1");//only send NotificationsDic. ADD ("Messages","{\ "title\": \ ""+ title +"\ ", \" description\ ": \""+ Description +"\"}"); Dic. ADD ("Msg_keys", Msg_keys);//Messages that identify the same message are overwritten automatically by the message. Only Android is supported. Dic. ADD (" Sign", Structsign ("POST", URL, dic, Secret_key)); StringBuilder SB=NewStringBuilder (); foreach(varDinchdic) {sb. Append (D.key+"="+ D.value +"&"); } sb. Remove (sb.) Length-1,1); byte[] data =Encoding.UTF8.GetBytes (sb.) ToString ()); WebClient WebClient=NewWebClient (); Try{WEBCLIENT.HEADERS.ADD ("Content-type","application/x-www-form-urlencoded");//To take the post must be added to the header, if you change to get a way to remove this sentence can be byte[] response = webclient.uploaddata (URL,"POST", data); returnEncoding.UTF8.GetString (response); } Catch(WebException ex) {stream stream=Ex. Response.getresponsestream (); byte[] bs =New byte[ the]; inti =0; intb; while((b = stream.) ReadByte ()) >0) {bs[i++] = (byte) b; } stream. Close (); returnEncoding.UTF8.GetString (BS,0, i); } } /// <summary> ///Build Sign/// </summary> /// <param name= "HttpMethod" ></param> /// <param name= "url" ></param> /// <param name= "dic" ></param> /// <param name= "Secret_key" ></param> /// <returns></returns> Private Static stringStructsign (stringHttpMethod,stringURL, dictionary<string,string> DiC,stringSecret_key) { //by key positive sequenceDIC = dic. (p = p.key). ToDictionary (p = p.key, p =p.value); StringBuilder SB=NewStringBuilder (); //Building Key-value pairs foreach(varDinchdic) {sb. Append (D.key+"="+d.value); } stringSign = httpmethod + URL + SB +Secret_key; //URL encodingSign =Sign . UrlEncode (); //capitalize the characters replaced by the code, this place is a pit ... Char[] cs =New Char[Sign. Length]; for(inti =0; I < sign. Length; i++) {Cs[i]=Sign[i]; if(Sign[i] = ='%') {cs[++i] =Convert.tochar (Sign[i]. ToString (). ToUpper ()); cs[++i] =Convert.tochar (Sign[i]. ToString (). ToUpper ()); } } //MD5 Encryption return New string(CS). MD5 (); } }
Plus two additional extension methods
System.Web.HttpUtility.UrlEncode need to add a reference system.web (. NET Framework 4.0 Client Profile cannot find system.web please switch to the. NET Framework 4.0)
/// <summary> ///MD5 Encryption/// </summary> /// <param name= "str" ></param> /// <returns></returns> Public Static stringMD5 ( This stringstr) { byte[] bs =NewMD5CryptoServiceProvider (). ComputeHash (Encoding.UTF8.GetBytes (str)); returnBitconverter.tostring (BS). Replace ("-",""); } /// <summary> ///URL encoding/// </summary> /// <param name= "str" ></param> /// <returns></returns> Public Static stringUrlEncode ( This stringstr) { returnSystem.Web.HttpUtility.UrlEncode (str, ENCODING.UTF8); }
C # simple Baidu push Code