<%@ WebHandler Language="C#" Class="getMeta" %> using System; using System.Web; using System.Runtime.Serialization.Json; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Web.Services.Protocols; using System.Security.Cryptography; using System.Text; using System.Web.SessionState; using System.Data; using System.Collections.Generic; using System.IO.Compression; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text.RegularExpressions; public class getMeta : IHttpHandler, IReadOnlySessionState { private string _baseurl = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath.TrimEnd('/'); public void ProcessRequest (HttpContext context) { result objRet = new result(); DataContractJsonSerializer json = new DataContractJsonSerializer(objRet.GetType()); context.Response.ContentType = "application/json;charset=utf-8"; context.Response.AddHeader("Access-Control-Allow-Origin", "*"); string acceptEncoding = context.Request.Headers["Accept-Encoding"].ToString().ToUpperInvariant(); if (!String.IsNullOrEmpty(acceptEncoding)) { if (acceptEncoding.Contains("GZIP")) { //输出流头部GZIP压缩 context.Response.AppendHeader("Content-encoding", "gzip"); context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress); } else if (acceptEncoding.Contains("DEFLATE")) { //输出流头部DEFLATE压缩 context.Response.AppendHeader("Content-encoding", "deflate"); context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress); } } authPermission objAuth = new authPermission(); if (!objAuth.isLogin()) { objRet.ret = "no"; objRet.err_code = "0001"; objRet.message = "尚未登入,請登入後使用"; json.WriteObject(context.Response.OutputStream, objRet); return; } string url = (context.Request["url"] == null) ? "" : context.Request["url"].ToString(); if (url == "") { objRet.ret = "no"; objRet.err_code = "0001"; objRet.message = "無傳入網址!\n"; json.WriteObject(context.Response.OutputStream, objRet); return; } string htmlData = globalClass.getPageData(url); //htmlData = htmlData.Replace("\n", ""); Match ogTitleMatch = Regex.Match(htmlData, "", RegexOptions.IgnoreCase | RegexOptions.Multiline); Match ogDescMatch = Regex.Match(htmlData, "", RegexOptions.IgnoreCase | RegexOptions.Multiline); Match ogImageMatch = Regex.Match(htmlData, "", RegexOptions.IgnoreCase | RegexOptions.Multiline); string ogTitle = ogTitleMatch.Groups[1].Value; string ogDesc = ogDescMatch.Groups[1].Value; string ogImage = ogImageMatch.Groups[1].Value; if (ogTitle == "") { ogTitleMatch = Regex.Match(htmlData, "", RegexOptions.IgnoreCase | RegexOptions.Multiline); ogDescMatch = Regex.Match(htmlData, "", RegexOptions.IgnoreCase | RegexOptions.Multiline); ogImageMatch = Regex.Match(htmlData, "", RegexOptions.IgnoreCase | RegexOptions.Multiline); ogTitle = ogTitleMatch.Groups[1].Value; ogDesc = ogDescMatch.Groups[1].Value; ogImage = ogImageMatch.Groups[1].Value; } objRet.ret = "yes"; objRet.ogTitle = ogTitle; objRet.ogDescript = ogDesc; if (objRet.ogDescript.Length > 100) { objRet.ogDescript = objRet.ogDescript.Substring(0, 95) + "..."; } string imageName = ""; if (ogImage != "") { imageName = globalClass.CreateRandomCode(24); string filePath = string.Format("/downloads/{0}.png", imageName); string path = System.Web.HttpContext.Current.Server.MapPath("~/"); try { globalClass.getUrlPic(ogImage).Save(path + filePath.Replace("/", "\\"), ImageFormat.Png); objRet.ogImage = _baseurl + "/downloads/" + imageName + ".png"; } catch (Exception ex) { objRet.ogImage = ""; } } json.WriteObject(context.Response.OutputStream, objRet); return; } public class result { public string ret = "no"; public string err_code = "0000"; public string message = ""; public string ogTitle = ""; public string ogDescript = ""; public string ogImage = ""; } public bool IsReusable { get { return false; } } }