///
/// 通过 一个 URL 生成 新浪 短连接 t.cn
///
///
///
public static string GetSinaShortUrlWith(string url)
{
string apiUrl = string.Format(“http://api.t.sina.com.cn/short_url/shorten.json?source=2785173621&url_long={0}”, url);
string json = WebData(apiUrl);
if (json != "")
{
json = "{ \"value\":" + json + "}";
}
SinaShortUrl ssu = Hy.Common.JSONUtil.Dencode<SinaShortUrl>(json);
if (ssu != null)
{
if (ssu.Value.First().url_short == "")
{
return url;
}
else
{
return ssu.Value.First().url_short;
}
}
return url;
}
/// <summary>
/// 获取 web 数据
/// </summary>
/// <param name="p_Url"></param>
/// <returns></returns>
public static string WebData(string p_Url)
{
string p_CharSet = "utf-8";
string WebData = "";
try
{
//创建实例
WebClient web = new WebClient();
//web.Proxy =
byte[] Data = web.DownloadData(p_Url);
//下载数据并返回数组字节
WebData = Encoding.GetEncoding(p_CharSet).GetString(Data) + WebData;
}
catch
{
return "";
}
return WebData;
}
#region 新浪 短连接 生成 json 解析 实体类
public class SinaShortUrl
{
public SinaUrl[] Value { get; set; }
}
public class SinaUrl
{
public string url_short { get; set; }
public string url_long { get; set; }
public int type { get; set; }
}
#endregion