当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现类似新浪微博长URL转短地址的方法

C#实现类似新浪微博长URL转短地址的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#实现类似新浪微博长url转短地址的方法。分享给大家供大家参考。具体如下:

一、前台判断用户输入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 () { 
  } 
 }); 
}

二、前台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="#"> 音乐</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>

三、以上是用来判断用户输入内容里面是否含有网址,下面是后台提交到数据库的时候进行的转换

#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.replace(mc[i].value, m_url.turl); 
     
    if(!b_url.exists(m_url.backup1)) 
    { 
     b_url.add(m_url); 
    } 
   } 
  } 
#endregion 
#region 写入微博数据库 
--写入微博数据库的代码 
#endregion

四、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

五、短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.replace(mc[i].value, "链接已经被屏蔽"); 
     } 
     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 

六、数据库结构(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#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网