UrlDecode

C# 专栏收录该内容
4 篇文章 0 订阅

  private void button1_Click(object sender, System.EventArgs e)
  {
   string test = "%C4%E3%BA%C3%B0%A1+sdfsfa%26";
   MessageBox.Show(UrlDecode(test));
  }

  string HexToStr(string HexSource)
  {   
   string rltstr = "";
   byte[] rltByte = new byte[HexSource.Length / 2];
   for(int i=0; i<HexSource.Length; i+=2)
   {
    string str = Convert.ToInt32(HexSource.Substring(i, 2), 16).ToString();
    rltByte[i/2] = Convert.ToByte(HexSource.Substring(i, 2), 16);
   }
   rltstr = System.Text.Encoding.Default.GetString(rltByte);
   return rltstr;
 
  }


  string UrlDecode(string source)
  {
   string deStr = "";
   string tmpStr = "";
   string tmpStr2 = "";
   string unSafestr = "!/"#$%&'()*+,/:;<=>?@[//]^`{|}~";
   for (int i = 0; i < source.Length -1; i++)
   {
    tmpStr = source.Substring(i,1);
    if (tmpStr == "%")
    {
     tmpStr2 = source.Substring(i+1, 2);
     if (unSafestr.IndexOf(HexToStr(tmpStr2)) != -1)
     {
      deStr += HexToStr(tmpStr2);
      i += 2;
     }
     else
     {
      tmpStr2 = source.Substring(i+1, 2) + source.Substring(i+4, 2);
      deStr += HexToStr(tmpStr2);
      i += 5;
     }
    }
    else
    {
     if (tmpStr == "+")
      deStr += " ";
     else
      deStr += tmpStr;
    }
   }
   return deStr;
  }

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值