Case: After the customer sign, the system will reply to a text message to the customer and a review service link, because the link has long, SMS will be automatically truncated, so the use of short-chain way to send.
The code is as follows:
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>
Parsing JSON strings
</summary>
<param name= "Pjsonstr" > string to parse such as: [{...,...,...}] </param>
<returns> Return Data collection </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);
Remove [{and}]
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;
Resolving key-value pairs
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;
}
The above is to generate short URL code, we hope to help. More. NET Tutorials: net.itcast.cn/net/video.shtml
This article is from the "World of Books" blog, please be sure to keep this source http://10221941.blog.51cto.com/10211941/1650053
Short URL code can also be generated using the Baidu API