C#完成相似新浪微博長URL轉短地址的辦法。本站提示廣大學習愛好者:(C#完成相似新浪微博長URL轉短地址的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成相似新浪微博長URL轉短地址的辦法正文
本文實例講述了C#完成相似新浪微博長URL轉短地址的辦法。分享給年夜家供年夜家參考。詳細以下:
1、前台斷定用戶輸出URL的JS代碼以下。
function CheckInput() { var $txtLength = $("#inp_text").val().length; if ($txtLength > 10) { var url = $("#inp_text").val(); var xx = url.match(RegExp("((news|telnet|nttp|file|http|ftp|https)://){1}(([-A-Za-z0-9]+(\\.[-A-Za-z0-9]+)*(\\.[-A-Za-z]{2,5}))|([0-9]{1,3}(\\.[0-9]{1,3}){3}))(:[0-9]*)?(/[-A-Za-z0-9_\\$\\.\\+\\!\\*\\(\\),;:@&=\\?/~\\#\\%]*)*", "gi") || []); if (xx != null) { for (var i = 0; i < xx.length; i++) { var $txtLength = $("#inp_text").val().length; $txtLength = $txtLength - xx[i].length + 11; } } } if ($txtLength < 141) { $("#div_txtlength").html("還能輸出<span>" + (140 - $txtLength) + "</span>個字"); } else { $("#div_txtlength").html("超越<span>" + ($txtLength - 140) + "</span>個字"); } } function InsertText() { if ($("#inp_text").val().Trim().length == 0) { art.dialog({ title: '毛病', icon: 'error', content: '請輸出內容', width: "150px", height: "80px", lock: true }); return; } //長url轉換成短url var url = $("#inp_text").val(); var xx = url.match(RegExp("((news|telnet|nttp|file|http|ftp|https)://){1}(([-A-Za-z0-9]+(\\.[-A-Za-z0-9]+)*(\\.[-A-Za-z]{2,5}))|([0-9]{1,3}(\\.[0-9]{1,3}){3}))(:[0-9]*)?(/[-A-Za-z0-9_\\$\\.\\+\\!\\*\\(\\),;:@&=\\?/~\\#\\%]*)*", "gi") || []); var $txtLength = $("#inp_text").val().length; if (xx != null) { for (var i = 0; i < xx.length; i++) { $txtLength = $txtLength - xx[i].length + 11; } } if ($txtLength < 141) { $("#div_txtlength").html("還能輸出<span>" + (140 - $txtLength) + "</span>個字"); } else { $("#div_txtlength").html("超越<span>" + ($txtLength - 140) + "</span>個字"); } if ($txtLength > 140) { art.dialog({ title: '毛病', icon: 'error', content: '字數超越限制', width: "150px", height: "80px", lock: true }); return false; } $.ajax({ type: "POST", url: "../MiniBlog/Handler.ashx", data: { "txt": $("#inp_text").val() }, datatype: "html", beforeSend: function () { $("#div_txtlength").html("正在提交。。。"); }, success: function (data) { if (data.length > 1) { window.location.reload(); } else { art.dialog({ title: '毛病', icon: 'error', content: '宣布掉敗,請復制內容後刷新以後頁面。', width: "150px", height: "80px", lock: true }); } }, complete: function (XMLHttpRequest, textStatus) { // alert(XMLHttpRequest.responseText); // alert(textStatus); }, error: function () { } }); }
2、前台ASPX的代碼以下(部門)
<div class="title_left"> 有甚麼新穎事和年夜家分享?</div> <div class="left_box"> <textarea class="textarea01" id="inp_text" onblur="CheckInput()" onkeyup="CheckInput()"> </textarea></div> <div class="left_box"> <div class="insert" > <ul> <li > <a href="#"> 臉色</a></li> <li > <a href="#"> 圖片</a></li> <li > <a href="#"> 音樂</a></li> </ul> </div> <div class="Prompt" id="div_txtlength"> 還能輸出<span>140</span>字</div> <div class="bottom_gb"> <a href="javascript:void(0)" onclick="InsertText();" class="link1"></a> </div> </div>
3、以上是用來斷定用戶輸出內容外面能否含有網址,上面是後台提交到數據庫的時刻停止的轉換
#region 長url轉短url Regex rx = new Regex("((news|telnet|nttp|file|http|ftp|https)://){1}(([-A-Za-z0-9]+(\\.[-A-Za-z0-9]+)*(\\.[-A-Za-z]{2,5}))|([0-9]{1,3}(\\.[0-9]{1,3}){3}))(:[0-9]*)?(/[-A-Za-z0-9_\\$\\.\\+\\!\\*\\(\\),;:@&=\\?/~\\#\\%]*)*", RegexOptions.IgnoreCase); string txt_context = context.Request.Form["txt"].ToString(); MatchCollection mc = rx.Matches(txt_context); if (mc.Count > 0) { for (int i = 0; i < mc.Count; i++) { Haedu.Gxt.Model.MINIBLOGURL_Model M_url = new Haedu.Gxt.Model.MINIBLOGURL_Model(); Haedu.Gxt.Bll.MINIBLOGURL B_url = new Haedu.Gxt.Bll.MINIBLOGURL(); M_url.BACKUP1 = Common.md5(mc[i].Value); M_url.BACKUP2 = " "; M_url.CREATETIME = DateTime.Now; M_url.CREATEUSER = User_BaseInfo.GetUserID; M_url.ID = Common.GetGUID; M_url.STATE = 0; M_url.SURL = mc[0].Value; M_url.TURL = MiniBlog.ShortUrl(mc[i].Value); txt_context = txt_context.WordStr(mc[i].Value, M_url.TURL); if(!B_url.Exists(M_url.BACKUP1)) { B_url.Add(M_url); } } } #endregion #region 寫入微博數據庫 --寫入微博數據庫的代碼 #endregion
4、MiniBlog.ShortUrl辦法代碼
#region 長轉短url /// <summary> /// 長url轉短url /// </summary> /// <param name="url">原url</param> /// <returns>前往短url</returns> public static string ShortUrl(string url) { //可以自界說生成MD5加密字符傳前的混雜KEY string key = "Haedu_MiniBlog"; //要應用生成URL的字符 string[] chars = new string[]{ "a","b","c","d","e","f","g","h", "i","j","k","l","m","n","o","p", "q","r","s","t","u","v","w","x", "y","z","0","1","2","3","4","5", "6","7","8","9","A","B","C","D", "E","F","G","H","I","J","K","L", "M","N","O","P","Q","R","S","T", "U","V","W","X","Y","Z"}; //對傳入網址停止MD5加密 string hex = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key + url, "md5"); string[] resUrl = new string[4]; for (int i = 0; i < 4; i++) { //把加密字符依照8位一組16進制與0x3FFFFFFF停止位與運算 int hexint = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16); string outChars = string.Empty; for (int j = 0; j < 6; j++) { //把獲得的值與0x0000003D停止位與運算,獲得字符數組chars索引 int index = 0x0000003D & hexint; //把獲得的字符相加 outChars += chars[index]; //每次輪回按位右移5位 hexint = hexint >> 5; } //把字符串存入對應索引的輸入數組 resUrl[i] = outChars; } return "http://url.cn/" + resUrl[(new Random()).Next(0, 3)]; } #endregion
5、短URL轉換成原始URL
#region 短url調換成原始url public static string CheckUrl(string context) { Regex rx = new Regex("((news|telnet|nttp|file|http|ftp|https)://){1}(([-A-Za-z0-9]+(\\.[-A-Za-z0-9]+)*(\\.[-A-Za-z]{2,5}))|([0-9]{1,3}(\\.[0-9]{1,3}){3}))(:[0-9]*)?(/[-A-Za-z0-9_\\$\\.\\+\\!\\*\\(\\),;:@&=\\?/~\\#\\%]*)*", RegexOptions.IgnoreCase); MatchCollection mc = rx.Matches(context); if (mc.Count > 0) { for (int i = 0; i < mc.Count; i++) { Haedu.Gxt.Model.MINIBLOGURL_Model M_url = new Haedu.Gxt.Model.MINIBLOGURL_Model(); Haedu.Gxt.Bll.MINIBLOGURL B_url = new Haedu.Gxt.Bll.MINIBLOGURL(); M_url = B_url.GetModel(mc[i].Value); if (M_url != null) { if (int.Parse(M_url.STATE.ToString()) == 2) { context = context.WordStr(mc[i].Value, "鏈接曾經被屏障"); } else { context = context.WordStr(mc[i].Value, "<a href=\"" + M_url.SURL + "\" target=\"_blank\" title=\"" + M_url.SURL + "\" >" + mc[i].Value + "</a>"); } } } } return context; } #endregion
6、數據庫構造(oracle)
-- Create table create table MINIBLOGURL ( id VARCHAR2(50) not null, surl VARCHAR2(200) not null, turl VARCHAR2(100) not null, createtime DATE not null, createuser VARCHAR2(50) not null, state NUMBER(1) not null, backup1 VARCHAR2(200) not null, backup2 VARCHAR2(200) not null ) tablespace TAB_GXT pctfree 10 initrans 1 maxtrans 255 storage ( initial 64K next 8K minextents 1 maxextents unlimited ); -- Add comments to the columns comment on column MINIBLOGURL.id is '邏輯ID'; comment on column MINIBLOGURL.surl is '原始url'; comment on column MINIBLOGURL.turl is '轉成的短url'; comment on column MINIBLOGURL.createtime is '創立時光'; comment on column MINIBLOGURL.createuser is '創立人ID'; comment on column MINIBLOGURL.state is '狀況,0為認證的網址(比擬著名的網站域名),1為未認證的網址(小網站),2為鎖定不許可點擊(告白類的網址)'; comment on column MINIBLOGURL.backup1 is 'MD5值,用來比擬網址能否曾經存在'; comment on column MINIBLOGURL.backup2 is '備用字段2'; -- Create/Recreate primary, unique and foreign key constraints alter table MINIBLOGURL add constraint PK_ID primary key (ID) using index tablespace TAB_GXT pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K next 1M minextents 1 maxextents unlimited ); -- Create/Recreate indexes create index IX_CREATEUSER on MINIBLOGURL (CREATEUSER) tablespace TAB_GXT pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K next 1M minextents 1 maxextents unlimited ); create unique index IX_MD5 on MINIBLOGURL (BACKUP1) tablespace TAB_GXT pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K next 1M minextents 1 maxextents unlimited ); create index IX_SURL on MINIBLOGURL (SURL) tablespace TAB_GXT pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K next 1M minextents 1 maxextents unlimited ); create index IX_TURL on MINIBLOGURL (TURL) tablespace TAB_GXT pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K next 1M minextents 1 maxextents unlimited );
至此,基於下面的代碼便可完成微博的長短URL互相轉換,詳細運用的時刻還須要本身停止調劑修正。
願望本文所述對年夜家的C#法式設計有所贊助。