protected void btnsave_Click(object sender, EventArgs e) { Cms.BLL.wx_info wx = new Cms.BLL.wx_info(); Cms.Model.wx_info mwx = new Cms.Model.wx_info(); DataTable dt = wx.GetList("id=1").Tables[0]; bool bl = false; if (dt.Rows.Count > 0) { mwx = wx.GetModel(1); mwx.AppId = AppId.Text.Trim(); mwx.AppSecret = AppSecret.Text.Trim(); mwx.access_token = hdid.Text.Trim(); bl = wx.Update(mwx); } else { mwx.AppId = AppId.Text.Trim(); mwx.AppSecret = AppSecret.Text.Trim(); mwx.access_token = Gettoken(); if (wx.Add(mwx) > 0) { bl = true; } } if (bl) { Bind(); ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('设置成功!')</script>"); } else { ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('设置失败!')</script>"); } }
public string UploadMultimedia(string ACCESS_TOKEN, string Type) { string result = ""; string wxurl = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" + ACCESS_TOKEN + "&type=" + Type; string filepath = Server.MapPath("image") + "\\01.jpg"; //(本地服务器的地址) // string filepath = "http://www.muhn.org.cn/Upload/image/20140827/20140827100505_4660.jpg"; //WriteLog("上传路径:" + filepath); WebClient myWebClient = new WebClient(); myWebClient.Credentials = CredentialCache.DefaultCredentials; try { byte[] responseArray = myWebClient.UploadFile(wxurl, "POST", filepath); result = System.Text.Encoding.Default.GetString(responseArray, 0, responseArray.Length); if (result.IndexOf("42001") > -1) { string str = Gettoken(); Cms.BLL.wx_info wx = new Cms.BLL.wx_info(); Cms.Model.wx_info mwx = new Cms.Model.wx_info(); mwx = wx.GetModel(1); mwx.access_token = str; wx.Update(mwx); Bind(); UploadMultimedia(str, Type); } string[] s = result.Split(','); result = s[1].Replace("\"media_id\":\"", "").Replace("\"", ""); } catch (Exception ex) { result = "Error:" + ex.Message; } // WriteLog("上传MediaId:" + result); return(result); }
/// <summary> ///发送获取会员的请求 /// </summary> public static string setinfo(string posturl, string postData, string openid) { Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.UTF8; byte[] data = encoding.GetBytes(postData); // 准备请求... try { // 设置参数 request = WebRequest.Create(posturl) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); string err = string.Empty; if (content.IndexOf("42001") > -1) { string str = Gettoken(); Cms.BLL.wx_info wx = new Cms.BLL.wx_info(); Cms.Model.wx_info mwx = new Cms.Model.wx_info(); mwx = wx.GetModel(1); mwx.access_token = str; wx.Update(mwx); setinfo("https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + str + "&openid=" + openid, postData, openid); } return(content); } catch (Exception ex) { string err = ex.Message; return(string.Empty); } }
protected void btnsave_Click(object sender, EventArgs e) { Cms.BLL.wx_info wx = new Cms.BLL.wx_info(); Cms.Model.wx_info mwx = new Cms.Model.wx_info(); DataTable dt = wx.GetList("id=1").Tables[0]; bool bl = false; if (dt.Rows.Count > 0) { mwx = wx.GetModel(1); mwx.AppId = AppId.Text.Trim(); mwx.AppSecret = AppSecret.Text.Trim(); mwx.access_token = hdid.Text.Trim(); mwx.url = wurl.Text.Trim(); mwx.wxid = WxId.Text.Trim(); adminUser.AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), mwx.AppId); //记录日志 bl = wx.Update(mwx); } else { mwx.AppId = AppId.Text.Trim(); mwx.AppSecret = AppSecret.Text.Trim(); mwx.access_token = hdid.Text.Trim(); mwx.url = wurl.Text.Trim(); mwx.wxid = WxId.Text.Trim(); if (wx.Add(mwx) > 0) { adminUser.AddAdminLog(DTEnums.ActionEnum.Add.ToString(), mwx.AppId); //记录日志 bl = true; } } if (bl) { Bind(); ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('设置成功!');", true); // ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('设置成功!')</script>"); } else { ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('设置失败!');", true); //ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('设置失败!')</script>"); } }
public string HttpUploadFile(string postData) { Cms.BLL.wx_info wxinfo = new Cms.BLL.wx_info(); Cms.Model.wx_info Model = new Cms.Model.wx_info(); Model = wxinfo.GetModel(1); string result = ""; string posturl = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" + Model.access_token + "&type=voice"; string filepath = Server.MapPath("~" + postData); WebClient myWebClient = new WebClient(); myWebClient.Credentials = CredentialCache.DefaultCredentials; try { byte[] responseArray = myWebClient.UploadFile(posturl, "POST", filepath); result = System.Text.Encoding.Default.GetString(responseArray, 0, responseArray.Length); if (result.IndexOf("42001") > -1) { string str = Gettoken(); Cms.BLL.wx_info wx = new Cms.BLL.wx_info(); Cms.Model.wx_info mwx = new Cms.Model.wx_info(); mwx = wx.GetModel(1); mwx.access_token = str; wx.Update(mwx); HttpUploadFile(postData); } string s = result.Replace("{", "").Replace("}", "").Replace("\"", ""); string[] ss = s.Split(','); result = ss[1].Replace("media_id:", ""); } catch (Exception ex) { result = "Error:" + ex.Message; } return(result); }
protected void btnts_Click(object sender, EventArgs e) { Cms.BLL.C_article cb = new Cms.BLL.C_article(); int id = int.Parse(tbid.Text.Trim() == ""?"0":tbid.Text.Trim()); if (id == 0) { return; } DataTable dt = cb.GetList("articleId=" + id).Tables[0]; if (dt.Rows.Count == 0) { ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "", "alert('文章不存在!');", true); return; } DataRow dr = dt.Rows[0]; string strurl = tburls.Text.Trim(); if (strurl == "") { ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "", "alert('请输入文章路径!');", true); return; } string[] suser = GetPage4("https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + hdid.Text, "").Split(','); //string str = UploadMultimedia(hdid.Text, "image"); string surl = "http://" + HttpContext.Current.Request.Url.Host.ToString(); string spic = surl + dr["photourl"].ToString(); foreach (string s in suser) { if (s == "") { continue; } StringBuilder sb = new StringBuilder(); //sb.Append("{"); //sb.Append("\"touser\":"+s+","); //sb.Append("\"msgtype\":\"image\","); //sb.Append("\"image\":"); //sb.Append("{"); //sb.Append("\"media_id\":\"" + str + "\""); //sb.Append("}"); //sb.Append("}"); sb.Append("{"); sb.Append("\"touser\":" + s + ","); sb.Append("\"msgtype\":\"news\","); sb.Append("\"news\":{"); sb.Append("\"articles\":["); sb.Append("{"); sb.Append("\"title\":\"" + dr["title"].ToString() + "\","); sb.Append("\"description\":\"" + dr["intro"].ToString() + "\","); sb.Append("\"url\":\"" + strurl + "\","); sb.Append("\"picurl\":\"" + spic + "\""); sb.Append(" }"); sb.Append("]"); sb.Append("}"); sb.Append("}"); setinfo("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + hdid.Text.Trim(), sb.ToString()); } //StringBuilder sb = new StringBuilder(); //string str = UploadMultimedia(hdid.Text, "image"); //sb.Append("{"); //sb.Append("\"articles\": ["); //sb.Append("{"); //sb.Append("\"thumb_media_id\":\"" + str + "\","); //sb.Append("\"author\":\"\","); //sb.Append("\"title\":\"小额贷款审计我之见\","); //sb.Append("\"content_source_url\":\"http://www.muhn.org.cn/contents9.html\","); //sb.Append("\"content\":\"自2008年银监会、央行发布《关于小额贷款公司试点的指导意见》以来\","); //sb.Append("\"digest\":\"digest\","); //sb.Append("\"show_cover_pic\":\"1\""); //sb.Append("}"); //sb.Append("]"); //sb.Append("}"); //GetPage3("https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token="+hdid.Text, sb.ToString(), 1); Cms.BLL.wx_info wx = new Cms.BLL.wx_info(); Cms.Model.wx_info mwx = new Cms.Model.wx_info(); dt = wx.GetList("id=2").Tables[0]; bool bl = false; if (dt.Rows.Count > 0) { mwx = wx.GetModel(2); mwx.AppId = tbid.Text.Trim(); mwx.AppSecret = tburls.Text.Trim(); bl = wx.Update(mwx); } else { mwx.AppId = tbid.Text.Trim(); mwx.AppSecret = tburls.Text.Trim(); if (wx.Add(mwx) > 0) { bl = true; } } ScriptManager.RegisterStartupScript(UpdatePanel2, UpdatePanel2.GetType(), "", "alert('推送成功!');", true); }
public string GetPage2(string posturl, string postData) { Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.UTF8; byte[] data = encoding.GetBytes(postData); // 准备请求... try { // 设置参数 request = WebRequest.Create(posturl) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); string err = string.Empty; if (content.IndexOf("42001") > -1) { string str = Gettoken(); Cms.BLL.wx_info wx = new Cms.BLL.wx_info(); Cms.Model.wx_info mwx = new Cms.Model.wx_info(); mwx = wx.GetModel(1); mwx.access_token = str; wx.Update(mwx); GetPage2("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + str, setmenu()); Bind(); //ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('停用失败!" + content + "')</script>"); // Response.Write("<script>alert('停用失败!" + content + "');window.location.href ='menu.aspx'</script>"); ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "", "alert('停用失败!');", true); } if (content.IndexOf("ok") > -1) { //ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('停用成功!')</script>"); ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "", "alert('停用成功!');", true); // Response.Write("<script>alert('停用成功!');window.location.href ='menu.aspx'</script>"); } // Response.Write(content); return(content); } catch (Exception ex) { string err = ex.Message; return(string.Empty); } }
public string GetPage4(string posturl, string postData) { Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.UTF8; byte[] data = encoding.GetBytes(postData); // 准备请求... try { // 设置参数 request = WebRequest.Create(posturl) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); string err = string.Empty; if (content.IndexOf("42001") > -1) { string str = Gettoken(); Cms.BLL.wx_info wx = new Cms.BLL.wx_info(); Cms.Model.wx_info mwx = new Cms.Model.wx_info(); mwx = wx.GetModel(1); mwx.access_token = str; wx.Update(mwx); Bind(); GetPage4("https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + str, postData); } if (content.IndexOf("openid") > -1) { string[] s = content.Split('['); string[] scount = s[0].Split(','); int stotal = int.Parse(scount[0].Replace("\"", "").Replace("total:", "").Replace("{", "")); string[] s2 = s[1].Split(']'); content = s2[0].ToString(); string snextid = s2[1].Replace("\"", "").Replace("}", "").Replace("next_openid:", ""); if (stotal > 10000 && snextid.Length > 1) { content = content + "," + GetPage4("https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + hdid.Text + "&next_openid=" + snextid + "", ""); } } return(content); } catch (Exception ex) { string err = ex.Message; return(string.Empty); } }
public string GetPage3(string posturl, string postData, int type) { Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.UTF8; byte[] data = encoding.GetBytes(postData); // 准备请求... try { // 设置参数 request = WebRequest.Create(posturl) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); string err = string.Empty; if (content.IndexOf("42001") > -1) { string str = Gettoken(); Cms.BLL.wx_info wx = new Cms.BLL.wx_info(); Cms.Model.wx_info mwx = new Cms.Model.wx_info(); mwx = wx.GetModel(1); mwx.access_token = str; wx.Update(mwx); Bind(); //if (type == 0) //{ // GetPage3("http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" + str + "&type=image", postData, type); //} if (type == 1) { GetPage3("https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=" + str, postData, type); } } string[] s = content.Split(','); string contents = s[1].Replace("\"media_id\":\"", "").Replace("\"", ""); if (content.IndexOf("media_id") > -1) { StringBuilder sb = new StringBuilder(); sb.Append("{"); sb.Append("\"touser\":["); sb.Append("" + GetPage4("https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + hdid.Text, "") + ""); sb.Append(" ],"); sb.Append("\"mpnews\":{"); sb.Append("\"media_id\":\"" + contents + "\""); sb.Append(" },"); sb.Append("\"msgtype\":\"mpnews\""); sb.Append("}"); string str = GetPage3("https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=" + hdid.Text + "", sb.ToString(), 0); } else if (content.IndexOf("0") > -1) { ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('推送成功!')</script>"); return(null); } else { ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('推送失败!" + content + "')</script>"); return(null); } //Response.Write(content); return(content); } catch (Exception ex) { string err = ex.Message; return(string.Empty); } }
public static string setinfo(string postData) { Cms.BLL.wx_info wxinfo = new Cms.BLL.wx_info(); Cms.Model.wx_info Model = new Cms.Model.wx_info(); Model = wxinfo.GetModel(1); //string postData = ""; string posturl = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=" + Model.access_token + "&type=voice"; Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.UTF8; byte[] data = encoding.GetBytes(postData); // 准备请求... try { // 设置参数 request = WebRequest.Create(posturl) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; request.ContentType = "audior"; request.ContentLength = data.Length; outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); string err = string.Empty; if (content.IndexOf("42001") > -1) { string str = Gettoken(); Cms.BLL.wx_info wx = new Cms.BLL.wx_info(); Cms.Model.wx_info mwx = new Cms.Model.wx_info(); mwx = wx.GetModel(1); mwx.access_token = str; wx.Update(mwx); setinfo(postData); } string s = content.Replace("{", "").Replace("}", "").Replace("\"", ""); string[] ss = s.Split(','); return(ss[1]); } catch (Exception ex) { string err = ex.Message; return(string.Empty); } }
public string GetPage3(string posturl, string postData) { Stream outstream = null; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.UTF8; byte[] data = encoding.GetBytes(postData); // 准备请求... try { // 设置参数 request = WebRequest.Create(posturl) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; outstream = request.GetRequestStream(); outstream.Write(data, 0, data.Length); outstream.Close(); //发送请求并获取相应回应数据 response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); //返回结果网页(html)代码 string content = sr.ReadToEnd(); string err = string.Empty; if (content.IndexOf("42001") > -1) { string str = Gettoken(); Cms.BLL.wx_info wx = new Cms.BLL.wx_info(); Cms.Model.wx_info mwx = new Cms.Model.wx_info(); mwx = wx.GetModel(1); mwx.access_token = str; wx.Update(mwx); Bind(); GetPage3("https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=" + str, postData); } if (content.IndexOf("0") > -1) { adminUser.AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "微信菜单推送成功"); //记录日志 ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('推送成功!');", true); //ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('推送成功!')</script>"); } else { adminUser.AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "微信菜单推送失败"); //记录日志 ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('推送失败!" + content + "');", true); // ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('推送失败!" + content + "')</script>"); } //Response.Write(content); return(content); } catch (Exception ex) { string err = ex.Message; return(string.Empty); } }