using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Specialized;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
|
|
namespace BO.Utils
|
|
{
|
|
public class UrlUtil
|
|
{
|
|
public static string GetBarCode(string barCode)
|
|
{
|
|
//如果是链接
|
|
//http://www.bwpsoft.com?code=WF20171108000003&name=
|
|
|
|
if (barCode.Contains("?") && barCode.ToLower().Contains("http"))
|
|
{
|
|
barCode = barCode.Replace("http://www.bwpsoft.com?code=", "");
|
|
var andIndex = barCode.IndexOf("&", StringComparison.Ordinal);
|
|
barCode = barCode.Substring(0, andIndex);
|
|
}
|
|
return barCode.Trim();
|
|
|
|
// if (barCode.Contains("?") && barCode.ToLower().Contains("http"))
|
|
// {
|
|
// Uri uri = new Uri(barCode);
|
|
// string queryString = uri.Query;
|
|
// NameValueCollection col = GetQueryString(queryString);
|
|
// string code = col["code"];
|
|
//
|
|
// return code;
|
|
// }
|
|
// else
|
|
// {
|
|
// return barCode;
|
|
// }
|
|
|
|
}
|
|
|
|
public static string GetGoodsName(string barCode)
|
|
{
|
|
//如果是链接
|
|
|
|
|
|
if (barCode.Contains("name="))
|
|
{
|
|
var index = barCode.IndexOf("name=", StringComparison.Ordinal);
|
|
|
|
barCode = barCode.Substring(index+5);
|
|
return barCode.Trim();
|
|
}
|
|
return "";
|
|
|
|
|
|
// if (barCode.Contains("?") && barCode.ToLower().Contains("http"))
|
|
// {
|
|
// Uri uri = new Uri(barCode);
|
|
// string queryString = uri.Query;
|
|
// NameValueCollection col = GetQueryString(queryString);
|
|
// string name = col["name"];
|
|
// return name;
|
|
// }
|
|
// else
|
|
// {
|
|
// return "";
|
|
// }
|
|
|
|
}
|
|
|
|
private static NameValueCollection GetQueryString(string queryString)
|
|
{
|
|
return GetQueryString(queryString, null, true);
|
|
}
|
|
|
|
private static NameValueCollection GetQueryString(string queryString, Encoding encoding, bool isEncoded)
|
|
{
|
|
queryString = queryString.Replace("?", "");
|
|
NameValueCollection result = new NameValueCollection(StringComparer.OrdinalIgnoreCase);
|
|
if (!String.IsNullOrEmpty(queryString))
|
|
{
|
|
int count = queryString.Length;
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
int startIndex = i;
|
|
int index = -1;
|
|
while (i < count)
|
|
{
|
|
char item = queryString[i];
|
|
if (item == '=')
|
|
{
|
|
if (index < 0)
|
|
{
|
|
index = i;
|
|
}
|
|
}
|
|
else if (item == '&')
|
|
{
|
|
break;
|
|
}
|
|
i++;
|
|
}
|
|
string key = null;
|
|
string value = null;
|
|
if (index >= 0)
|
|
{
|
|
key = queryString.Substring(startIndex, index - startIndex);
|
|
value = queryString.Substring(index + 1, (i - index) - 1);
|
|
}
|
|
else
|
|
{
|
|
key = queryString.Substring(startIndex, i - startIndex);
|
|
}
|
|
if (isEncoded)
|
|
{
|
|
result[(string) MyUrlDeCode(key, encoding)] = MyUrlDeCode(value, encoding);
|
|
}
|
|
else
|
|
{
|
|
result[key] = value;
|
|
}
|
|
if ((i == (count - 1)) && (queryString[i] == '&'))
|
|
{
|
|
result[key] = String.Empty;
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static string MyUrlDeCode(string str, Encoding encoding)
|
|
{
|
|
if (encoding == null)
|
|
{
|
|
Encoding utf8 = Encoding.UTF8;
|
|
//首先用utf-8进行解码
|
|
string code = HttpUtility.UrlDecode(str.ToUpper(), utf8);
|
|
//将已经解码的字符再次进行编码.
|
|
string encode = HttpUtility.UrlEncode(code, utf8).ToUpper();
|
|
if (str == encode)
|
|
encoding = Encoding.UTF8;
|
|
else
|
|
encoding = Encoding.GetEncoding("gb2312");
|
|
}
|
|
return HttpUtility.UrlDecode(str, encoding);
|
|
}
|
|
}
|
|
}
|