龙空技术网

永久图文素材代码

土哥编程 61

前言:

而今兄弟们对“regexjs”大约比较关切,姐妹们都需要了解一些“regexjs”的相关资讯。那么小编也在网上收集了一些对于“regexjs””的相关内容,希望各位老铁们能喜欢,大家快快来了解一下吧!

C#新增永久图文素材新增永久图文素材接口调用请求说明http请求方式: POST,https协议 同步公众号素材                    JavaScriptSerializer Jss = new JavaScriptSerializer();                    string respText = cwebcms.Web.user.BasicApi.WebRequestPostOrGet(string.Format(";appid={0}&secret={1}", AppID, AppSecret), "");                    Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText);                    string accessToken = respDic["access_token"].ToString();                    //上传封面图片获取media_id                    string ssPath = Server.MapPath(uc.PicURL);                    string re = cwebcms.Common.Public.add_material("image/jpeg", ssPath, accessToken);                    string thumb_media_id = "";                    respDic = (Dictionary<string, object>)Jss.DeserializeObject(re);                    if (respDic.ContainsKey("media_id"))                    {                        thumb_media_id = respDic["media_id"].ToString();                    }                    cwebcms.Common.Public.saveLogFiles(5, "re" + re, "errmsg:", "thumb_media_id:" + thumb_media_id);                    //上传图文消息内的图片获取URL//替换内容区的所有图片为素材中的图片                    string content = uc.Content;                    string pattern = "\\<img\\ [\\s\\S]*?src=['\"]?(?<f>[^'\"\\>\\ ]+)['\"\\>\\ ]";                    Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);                    Match match = reg.Match(content);                    string gPicURL = "", img_url = "";                    int Picnumj = 1;                    while (match.Success)                    {                        gPicURL = match.Groups["f"].Value;                        ssPath = Server.MapPath(gPicURL);                        re = cwebcms.Common.Public.uploadimg(ssPath, accessToken);                        respDic = (Dictionary<string, object>)Jss.DeserializeObject(re);                        if (respDic.ContainsKey("url"))                        {                            img_url = respDic["url"].ToString();                        }                        content = content.Replace(gPicURL, img_url);                        Picnumj++;                        match = match.NextMatch();                    }                    string url = "; + accessToken;                    string str = "{" +                                "\"articles\": [{" +                                 "\"title\": \"" + uc.NewsTitle + "\"," +                                "\"thumb_media_id\": \"" + thumb_media_id + "\"," +                                "\"author\": \"" + uc.Author + "\"," +                                "\"digest\": \"" + uc.naviContent + "\"," +                                "\"show_cover_pic\": 1," +                                "\"content\": \"" + content.Replace("\"", "\'") + "\"," +                                "\"content_source_url\": \";id=" + uc.ID + "\"," +                                "\"need_open_comment\":1," +                                "\"only_fans_can_comment\":1" +                                "\"}," +                                //若新增的是多图文素材,则此处应还有几段articles结构                                "\"]" +                                "\"}        ";                    cwebcms.Common.Public.saveLogFiles(5, "@url" + url, "errmsg:", "str:" + str);核心代码                    string media_id = "";                    string strReturn = cwebcms.Common.Public.PostUrl(@url, str);                    cwebcms.Common.Public.saveLogFiles(5, "n", "", "strReturn:" + strReturn);                    respDic = (Dictionary<string, object>)Jss.DeserializeObject(strReturn);                    if (respDic.ContainsKey("media_id"))                    {                        media_id = respDic["media_id"].ToString();                    }                    string sql = "update cwebcms_news set media_id='" + media_id + "' where NewsID='" + uc.NewsID + "'";                    int n = sd.updatesql(sql); //上传封面图片上传图文消息内的图片获取URL本接口所上传的图片不占用公众号的素材库中图片数量的100000个的限制。图片仅支持jpg/png格式,大小必须在1MB以下。接口调用请求说明http请求方式: POST,https协议  调用示例(使用curl命令,用FORM表单方式上传一个图片): curl -F media=@test.jpg ";public static  string uploadimg(string file,string accessToken)        {            string fileName = file;            string url = string.Format("{0}", accessToken);            FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read);            byte[] fileByte = new byte[fs.Length];            fs.Read(fileByte, 0, fileByte.Length);            fs.Close();            // 设置参数            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;            CookieContainer cookieContainer = new CookieContainer();            request.CookieContainer = cookieContainer;            request.AllowAutoRedirect = true;            request.Method = "POST";            string boundary = DateTime.Now.Ticks.ToString("X"); // 隨機分隔線            request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;            byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");            byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");            //请求头部信息            StringBuilder sbHeader =                new StringBuilder(                    string.Format(                        "Content-Disposition:form-data;name=\"media\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n",                        fileName));            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());            Stream postStream = request.GetRequestStream();            postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);            postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);            postStream.Write(fileByte, 0, fileByte.Length);            postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);            postStream.Close();             HttpWebResponse response = request.GetResponse() as HttpWebResponse;            Stream instream = response.GetResponseStream();            StreamReader sr = new StreamReader(instream, Encoding.UTF8);            string content = sr.ReadToEnd();            return content;        }新增其他类型永久素材(注意视频接口不可用,上传也不审核)接口调用请求说明通过POST表单来调用接口,表单id为media,包含需要上传的素材内容,有filename、filelength、content-type等信息。请注意:图片素材将进入公众平台官网素材管理模块中的默认分组。http请求方式: POST,需使用https ;type=TYPE 调用示例(使用curl命令,用FORM表单方式新增一个其他类型的永久素材,curl命令的使用请自行查阅资料)public static string add_material(string type, string file,  string accessToken)        {            string fileName = file;            string url = string.Format("{0}&type={1}", accessToken, type);            FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read);            byte[] fileByte = new byte[fs.Length];            fs.Read(fileByte, 0, fileByte.Length);            fs.Close();            // 设置参数            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;            CookieContainer cookieContainer = new CookieContainer();            request.CookieContainer = cookieContainer;            request.AllowAutoRedirect = true;            request.Method = "POST";            string boundary = DateTime.Now.Ticks.ToString("X"); // 隨機分隔線            request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;            byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n");            byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");            //请求头部信息            StringBuilder sbHeader =                new StringBuilder(                    string.Format(                        "Content-Disposition:form-data;name=\"media\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n",                        fileName));            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());            Stream postStream = request.GetRequestStream();            postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);            postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);            postStream.Write(fileByte, 0, fileByte.Length);            postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);            postStream.Close();            HttpWebResponse response = request.GetResponse() as HttpWebResponse;            Stream instream = response.GetResponseStream();            StreamReader sr = new StreamReader(instream, Encoding.UTF8);            string content = sr.ReadToEnd();            return content;        }//public static string PostUrl(string url, string postData)        {            string result = "";            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);            req.Method = "POST";            req.Timeout = 5000;//设置请求超时时间,单位为毫秒            req.ContentType = "application/json";            byte[] data = Encoding.UTF8.GetBytes(postData);            req.ContentLength = data.Length;            using (Stream reqStream = req.GetRequestStream())            {                reqStream.Write(data, 0, data.Length);                reqStream.Close();            }            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();            Stream stream = resp.GetResponseStream();            //获取响应内容            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))            {                result = reader.ReadToEnd();            }            return result;        }

标签: #regexjs