C#實(shí)現(xiàn)類似新浪微博長(zhǎng)URL轉(zhuǎn)短地址的方法
本文實(shí)例講述了C#實(shí)現(xiàn)類似新浪微博長(zhǎng)URL轉(zhuǎn)短地址的方法。分享給大家供大家參考。具體如下:
一、前臺(tái)判斷用戶輸入U(xiǎn)RL的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>個(gè)字");
}
else {
$("#div_txtlength").html("超出<span>" + ($txtLength - 140) + "</span>個(gè)字");
}
}
function InsertText() {
if ($("#inp_text").val().Trim().length == 0) {
art.dialog({
title: '錯(cuò)誤',
icon: 'error',
content: '請(qǐng)輸入內(nèi)容',
width: "150px",
height: "80px",
lock: true
});
return;
}
//長(zhǎng)url轉(zhuǎn)換成短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>個(gè)字");
}
else {
$("#div_txtlength").html("超出<span>" + ($txtLength - 140) + "</span>個(gè)字");
}
if ($txtLength > 140) {
art.dialog({
title: '錯(cuò)誤',
icon: 'error',
content: '字?jǐn)?shù)超出限制',
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: '錯(cuò)誤',
icon: 'error',
content: '發(fā)布失敗,請(qǐng)復(fù)制內(nèi)容后刷新當(dāng)前頁(yè)面。',
width: "150px",
height: "80px",
lock: true
});
}
},
complete: function (XMLHttpRequest, textStatus) {
// alert(XMLHttpRequest.responseText);
// alert(textStatus);
},
error: function () {
}
});
}
二、前臺(tái)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" style="visibility: hidden"> <ul> <li style="background: url(../images/weibo/icon.jpg) no-repeat -172px 0px;"> <a href="#"> 表情</a></li> <li style="background: url(../images/weibo/icon.jpg) no-repeat -115px 0px;"> <a href="#"> 圖片</a></li> <li style="background: url(../images/weibo/icon.jpg) no-repeat -229px 0px;"> <a href="#"> 音樂(lè)</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>
三、以上是用來(lái)判斷用戶輸入內(nèi)容里面是否含有網(wǎng)址,下面是后臺(tái)提交到數(shù)據(jù)庫(kù)的時(shí)候進(jìn)行的轉(zhuǎn)換
#region 長(zhǎng)url轉(zhuǎn)短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.Replace(mc[i].Value, M_url.TURL);
if(!B_url.Exists(M_url.BACKUP1))
{
B_url.Add(M_url);
}
}
}
#endregion
#region 寫(xiě)入微博數(shù)據(jù)庫(kù)
--寫(xiě)入微博數(shù)據(jù)庫(kù)的代碼
#endregion
四、MiniBlog.ShortUrl方法代碼
#region 長(zhǎng)轉(zhuǎn)短url
/// <summary>
/// 長(zhǎng)url轉(zhuǎn)短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"};
//對(duì)傳入網(wǎng)址進(jìn)行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進(jìn)制與0x3FFFFFFF進(jìn)行位與運(yùn)算
int hexint = 0x3FFFFFFF & Convert.ToInt32("0x" + hex.Substring(i * 8, 8), 16);
string outChars = string.Empty;
for (int j = 0; j < 6; j++)
{
//把得到的值與0x0000003D進(jìn)行位與運(yùn)算,取得字符數(shù)組chars索引
int index = 0x0000003D & hexint;
//把取得的字符相加
outChars += chars[index];
//每次循環(huán)按位右移5位
hexint = hexint >> 5;
}
//把字符串存入對(duì)應(yīng)索引的輸出數(shù)組
resUrl[i] = outChars;
}
return "http://url.cn/" + resUrl[(new Random()).Next(0, 3)];
}
#endregion
五、短URL轉(zhuǎn)換成原始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.Replace(mc[i].Value, "鏈接已經(jīng)被屏蔽");
}
else
{
context = context.Replace(mc[i].Value, "<a href=\"" + M_url.SURL + "\" target=\"_blank\" title=\"" + M_url.SURL + "\" >" + mc[i].Value + "</a>");
}
}
}
}
return context;
}
#endregion
六、數(shù)據(jù)庫(kù)結(jié)構(gòu)(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 '轉(zhuǎn)成的短url'; comment on column MINIBLOGURL.createtime is '創(chuàng)建時(shí)間'; comment on column MINIBLOGURL.createuser is '創(chuàng)建人ID'; comment on column MINIBLOGURL.state is '狀態(tài),0為認(rèn)證的網(wǎng)址(比較知名的網(wǎng)站域名),1為未認(rèn)證的網(wǎng)址(小網(wǎng)站),2為鎖定不允許點(diǎn)擊(廣告類的網(wǎng)址)'; comment on column MINIBLOGURL.backup1 is 'MD5值,用來(lái)比較網(wǎng)址是否已經(jīng)存在'; 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 );
至此,基于上面的代碼即可完成微博的長(zhǎng)短URL相互轉(zhuǎn)換,具體應(yīng)用的時(shí)候還需要自己進(jìn)行調(diào)整修改。
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
基于C#實(shí)現(xiàn)簡(jiǎn)單的音樂(lè)播放器
這篇文章主要介紹了如何基于C#實(shí)現(xiàn)簡(jiǎn)單的音樂(lè)播放器,考慮到需求中的界面友好和跨版本兼容性,我們可以選擇選擇Windows Forms作為開(kāi)發(fā)平臺(tái),Windows Forms提供了一個(gè)簡(jiǎn)單而強(qiáng)大的方法來(lái)創(chuàng)建桌面應(yīng)用程序,文中通過(guò)代碼示例給大家講解的非常詳細(xì),需要的朋友可以參考下2024-05-05
C# 多線程學(xué)習(xí)之基礎(chǔ)入門(mén)
線程(英語(yǔ):thread)是操作系統(tǒng)能夠進(jìn)行運(yùn)算調(diào)度的最小單位。它被包含在進(jìn)程之中,是進(jìn)程中的實(shí)際運(yùn)作單位。今天小編就帶大家了解一下C#中的多線程,快來(lái)學(xué)習(xí)一下吧2021-12-12
C#設(shè)計(jì)模式之Mediator中介者模式解決程序員的七夕緣分問(wèn)題示例
這篇文章主要介紹了C#設(shè)計(jì)模式之Mediator中介者模式解決程序員的七夕緣分問(wèn)題,簡(jiǎn)單說(shuō)明了中介者模式的定義并結(jié)合七夕緣分問(wèn)題實(shí)例分析了中介者模式的具體使用技巧,需要的朋友可以參考下2017-09-09
C#基于QRCode實(shí)現(xiàn)動(dòng)態(tài)生成自定義二維碼圖片功能示例
這篇文章主要介紹了C#基于QRCode實(shí)現(xiàn)動(dòng)態(tài)生成自定義二維碼圖片功能,結(jié)合實(shí)例形式分析了C#使用QRCode動(dòng)態(tài)生成二維碼圖片相關(guān)操作技巧,需要的朋友可以參考下2019-02-02
c# 插入數(shù)據(jù)效率測(cè)試(mongodb)
這篇文章主要介紹了c# 插入數(shù)據(jù)效率測(cè)試(mongodb),插入的速度要比Mysql和sqlserver都要快需要的朋友可以參考下2018-03-03

