您好,登錄后才能下訂單哦!
案例:客戶(hù)簽收后,系統(tǒng)會(huì)回復(fù)一條短信給客戶(hù)及一個(gè)點(diǎn)評(píng)服務(wù)的鏈接,由于鏈接過(guò)長(zhǎng),短信會(huì)自動(dòng)截?cái)?,所以使用短鏈的方式發(fā)送。
代碼如下:
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>返回?cái)?shù)據(jù)集合</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;
//解析鍵值對(duì)
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代碼,希望對(duì)大家有所幫助。 更多.net教程:net.itcast.cn/net/video.shtml
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。