您好,登錄后才能下訂單哦!
案例:客戶簽收后,系統會回復一條短信給客戶及一個點評服務的鏈接,由于鏈接過長,短信會自動截斷,所以使用短鏈的方式發送。
代碼如下:
public string GetTinyUrl(string strLongUrl)
{
try
{
byte[] postData = Encoding.UTF8.GetBytes("url=" + strLongUrl);
var url = "http://dwz.cn/create.php";
var client = new WebClient();
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] responseData = client.UploadData(url, "POST", postData);
var result = Encoding.UTF8.GetString(responseData);
IDictionary<string, string> dicResult = ParseSinge(result);
return dicResult["tinyurl"].Replace("\\", "");
}
catch (Exception)
{
return "";
}
}
/// <summary>
/// 解析JSON字符串
/// </summary>
/// <param name="pJsonStr">待解析的字符串形如:[{...,...,...}]</param>
/// <returns>返回數據集合</returns>
public IDictionary<string, string> ParseSinge(string pJsonStr)
{
if (pJsonStr.Trim().Length == 0 || pJsonStr.IndexOf("{") == -1 || pJsonStr.IndexOf("}") == -1) return null;
System.Text.StringBuilder sb = new System.Text.StringBuilder(pJsonStr);
//去除[{和}]
sb.Remove(0, 1);
sb.Remove(sb.Length - 1, 1);
string[] arr = sb.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
if (arr == null || arr.Length == 0) return null;
//解析鍵值對
Dictionary<string, string> dic = new Dictionary<string, string>();
string[] brr = null;
foreach (string str in arr)
{
brr = str.Split(new char[] { ':' }, 2);
dic.Add(brr[0].Replace("\"",""), brr[1].Replace("\"",""));
}
return dic;
}
以上為生成短URL代碼,希望對大家有所幫助。 更多.net教程:net.itcast.cn/net/video.shtml
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。